使用 SignalR 集线器可以在组中添加或删除客户端。一个客户端可以属于多个组。是否可以从当前所属的每个组中删除客户端?我想我正在寻找的是类似的东西Clients[*allgroups*].leave(Context.ConnectionId)
问问题
6628 次
2 回答
4
从 v0.5.2 开始,无法离开所有组,因为服务器不会跟踪客户端所属的组。您需要自己执行此操作,并从每个组中逐一删除客户端。
然而,在积压中有类似的请求,所以也许这将在未来的版本中实现:https ://github.com/SignalR/SignalR/issues/66
于 2012-07-20T16:22:43.567 回答
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);
}
}
于 2015-01-08T19:57:21.257 回答