user2965241
user2965241

Reputation: 53

SignalR missing client call methods

I need to send strings in realtime to clients, but my code only work with sleep. If I reduce the sleep parameter some calls are missed. My hub class is empty, because I don't need client-to-server calls. Here is my aspx file: Client (aspx):

var myhub = $.connection.chatHub;
myhub.client.broadcastMessage = function (message) {
    $('#container').append(message);
};
$.connection.hub.start();

Server (c#):

protected void Button1_Click(object sender, EventArgs e)
{            
    Thread t = new Thread(novaThread);
    t.Start();
}

static void novaThread() {
    var hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
    for (int i = 0; i < 20; i++)
    {
        hubContext.Clients.All.broadcastMessage("Server datetime: " + DateTime.Now.ToString());
        Thread.Sleep(1000);
    }   
}

Upvotes: 0

Views: 737

Answers (1)

David L
David L

Reputation: 33815

You still need the hub if you want to invoke client methods on the server. The hub is what perpetuates that connection. What it sounds like is that you need a bit of renaming.

var myhub = $.connection.chatHub;
myhub.client.broadcastMessage = function (message) {
        $('#container').append(message);
};

$.connection.hub.start();

testHub means absolutely nothing if you're invoking ChatHub on the server. As a result, I've updated your javascript to invoke chatHub, NOT testHub as you were doing. By simply making sure that you're creating an instance of chatHub in the client, you should be able to get rid of your Thread.Sleep(1000) (assuming you don't actually need it for something) and you'll be able to broadcast to the client perfectly.

EDIT:

In light of your comments, it may be that you're actually spinning up too many threads. Depending on how many threads you are running, you may need to raise the number of concurrent client-side connections per domain. The default is two. See http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.defaultconnectionlimit.aspx

Upvotes: 1

Related Questions