如何使用 userId 从组中添加/删除用户
Groups.Add(userid,"groupName")
我在我的数据库中使用而不是使用 connectionId
Groups.Add(connectionId,"groupName")
我创建了一个类似 here 的映射!使用 User ID Provider 方法,我能够做到这一点
Clients.Users(userId).sendMessage("asa")
但是
Groups.Add(userid,"groupName")
它不工作。 那么我怎样才能使 Groups.Add(userid,"groupName") 工作呢?是否有我不知道的特殊映射或者我使用错误?
最佳答案
无法将 userId
添加到组中。您应该只使用 connectionId
。
根据您的需要,您可以使用 the link you provided 中描述的另一种方法。 .
例如,您可以将每个连接添加到由userId
命名的组中:
Groups.Add(Context.ConnectionId, userId);
然后就可以给指定的用户发送消息了:
Clients.Group(userId).sendMessage("asa");
另一个用例可能包括在 OnConnected
方法中确定 userId
,然后通过其 connectionId
将用户添加到所需的组:
var groupName = GetGroupNameByUserId(userId);
Groups.Add(Context.ConnectionId, groupName);
...
Clients.Group(groupName).sendMessage("asa");
关于c# - 将 SignalR 用户映射到组中的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25358059/