Iterate through groups in signalR hub class

2019-09-01 08:56发布

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

2条回答
欢心
2楼-- · 2019-09-01 09:38

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.

查看更多
别忘想泡老子
3楼-- · 2019-09-01 09:44

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.

查看更多
登录 后发表回答