I’m working on an MVC3 project right now and just started using SignalR.
I have followed several demos but I can’t seem to get it working when my codes are inside \Views\Shared_Layout.cshtml. I always get the “Connection must be started before data can be sent.” error.
Please see my codes below:
My Class:
[HubName("notificationsHub")]
public class NotificationsHub : Hub
{
public void updateServer()
{
Clients.updateClient("boom");
}
}
My script inside _Layout.cshtml:
$(function(){
var signalR = $.connection.notificationsHub;
signalR.updateClient = function (message) {
alert(message);
};
$("#open").click(function () {
signalR.updateServer();
});
$.connection.hub.start(function () {
alert("Connected");
});
});
Script references:
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.6.4.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.signalR.js")"></script>
<script type="text/javascript" src="@Url.Content("~/signalr/hubs")"></script>
My NotificationsHub Class is inside a folder named NotificationsHub which is of the same directory level as my Views.
I tried this on a .html file and it works perfectly (I get the alert callback for $.connection.hub.start).
What am I missing here?
Thanks
That’s weird, I cannot reproduce the problem. Here’s my Layout:
Works perfectly fine. Make sure you don’t have script reference errors.