Using a SignalR hub clients can be added or removed from a group. A client can belong to multiple groups. Is it possible to remove a client from every group it currently belongs to? I guess what I'm looking for is something like Clients[*allgroups*].leave(Context.ConnectionId)
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Looks like they have yet to implement this, but it is considered a candidate for v3. A feature request with the following code exists at https://github.com/SignalR/SignalR/issues/66
public static class SignalRConnectionToGroupsMap
{
private static readonly ConcurrentDictionary<string, List<string>> Map = new ConcurrentDictionary<string, List<string>>();
public static bool TryAddGroup(string connectionId, string groupName)
{
List<string> groups;
if (!Map.TryGetValue(connectionId, out groups))
{
return Map.TryAdd(connectionId, new List<string>() {groupName});
}
if (!groups.Contains(groupName))
{
groups.Add(groupName);
}
return true;
}
// since for this use case we will only want to get the List of group names
// when we're removing the mapping - we might as well remove the mapping while
// we're grabbing the List
public static bool TryRemoveConnection(string connectionId, out List<string> result)
{
return Map.TryRemove(connectionId, out result);
}
}
回答2:
As of v0.5.2, there is no way to leave all groups because the server doesn't keep track of the groups a client belongs to. You need to do this yourself and remove the client from each group one-by-one.
There's a request for something similar in the backlog however, so maybe this will be implemented in a future release: https://github.com/SignalR/SignalR/issues/66