SignalR - 保留所有组

时间:2012-07-11 22:40:10

标签: asp.net signalr

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

2 个答案:

答案 0 :(得分:4)

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

但是积压请求类似的东西,所以也许这将在未来的版本中实现:https://github.com/SignalR/SignalR/issues/66

答案 1 :(得分: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);
    }
}