简体   繁体   中英

SignalR Counting Connections

I am trying to keep track of and count the number of connections connected to my signalR hub, however I am not doing it right.

What I have done is each time a client connects the tally gets incremented , so when I open other tab , I see that it knows there are 2 clients. However I would like the already connected clients to get the updated count which they do not (1st client sees "1") unless I refresh.

C#

public Task Connect()
{
    _connections.TryAdd(Context.ConnectionId, null);
    return Clients.tally(_connections.Count.ToString());
}

JS

chat.tally = function (message) {
    $('#tally').replaceWith(message);
};

I have looked at the Wiki but not getting how I implement the callback to all connected clients.

Your setup does not not appear to have any major issues, chat.tally() should be getting invoked on your connected clients with the correct parameter passed down. Perhaps message is not displaying as you intend. Instead of $('#tally').replaceWith(message); try $('#tally').html(message);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM