c# - 将 SignalR 用户映射到组中的连接

标签 c# asp.net .net signalr asp.net-identity

如何使用 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/

相关文章:

c# - 使用 Business Objects 的 DataGridView 列排序

asp.net - 如何将调试器附加到 IIS 而不是 ASP.NET 开发服务器?

asp.net - 如何使用 WSDL 文件使用 Web 服务?

c# - 如何通过 SQL 中的列名检查单元格是否为空?

c# - 使用 Graphics.MeasureString 进行字符串测量

c# - asp.net 用户控件事件传播问题

c# - 如何使用 GDI+ 绘制圆环( donut )?

c# - MS Access 查询设计器 - 使用标准 .NET 3.5 控件的类似功能?

c# - 运算符 '!' 不能应用于 x 类型的操作数

c# - 通过javascript保存对话框