Getting all group names in signalr

2019-03-01 00:45发布

问题:

ok so I have this code

var c = GlobalHost.ConnectionManager.GetHubContext<SomeHubClass>().Clients;

now from this the Clients returns an IHubConext that has IHubConnectionContext that has an IGroupManager Groups. now is there anyway to get all the group names from this? Is this even possible with the signalR interface or do I have to track all the groups for each hub myself?

回答1:

SignalR does not have an exposed API for managing groups as a whole, iterating over groups, or even obtaining a summary list of groups. You can only add or remove groups. If you want to persist a list of group names, perhaps use a singleton pattern for your SomeHubClass. Persist a List<string> of group names in the singleton that you can easily access, or even a Dictionary<string, HashSet<string>> to map both the name and the hashset of connection ids, though that is probably overkill in this instance.

See http://www.asp.net/signalr/overview/hubs-api/hubs-api-guide-server#callfromoutsidehub for implementing a singleton of your hub.



标签: c# signalr