SignalR - 离开所有组

Mar*_*ark 5 asp.net signalr

使用 SignalR 集线器可以在组中添加或删除客户端。一个客户端可以属于多个组。是否可以将客户端从其当前所属的每个组中删除?我想我正在寻找的是类似的东西Clients[*allgroups*].leave(Context.ConnectionId)

Ale*_*ger 5

从 v0.5.2 开始,无法离开所有组,因为服务器不跟踪客户端所属的组。您需要自己执行此操作,并将客户端从每个组中一一删除。

然而,积压中存在类似的请求,因此也许这将在未来的版本中实现: https: //github.com/SignalR/SignalR/issues/66


Jor*_*dan 2

看起来他们还没有实现这一点,但它被认为是 v3 的候选者。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);
    }
}
Run Code Online (Sandbox Code Playgroud)