SignalR - Leave All Groups

后端 未结 2 545
我在风中等你
我在风中等你 2021-01-19 09:45

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 bel

相关标签:
2条回答
  • 2021-01-19 10:08

    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

    0 讨论(0)
  • 2021-01-19 10:17

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题