Iterate through groups in signalR hub class

笑着哭i 提交于 2019-12-24 19:25:15

问题


How can I iterate through a SignalR group (hub class) Groups.Add(Context.ConnectionId, "foo");

How would I iterate through the group to see whose in it? and then possibly based on the connectionId in there return a user


回答1:


From the SignalR docs:

Groups are not persisted on the server so applications are responsible for keeping track of what connections are in what groups so things like group count can be achieved.

So no, you can't iterate over the users in a group, you need to keep track of that yourself.




回答2:


Possibly implement a Dictionary when the clients conn/dis/re-connect

public static readonly ConcurrentDictionary<string, object> _connections 
       = new ConcurrentDictionary<string, object>();

public Task Connect()
{
    _connections.TryAdd(Context.ConnectionId, null);
    Groups.Add(Context.ConnectionId, "users");
    //Returns Connection count. 
    return Clients.tally(_connections.Count.ToString());
}

You can expand this to include there name or group etc , but like akoeplinger say's you have to keep track of this throughout your app.



来源:https://stackoverflow.com/questions/11870698/iterate-through-groups-in-signalr-hub-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!