来自控制器的SignalR加入组 [英] SignalR Join Group From Controller
问题描述
当用户登录我的网站时,他们从下拉列表中选择他们所属的组.在他们登录后的登录回发中,我想将它们分配给正确的SignalR组.
When the user logs in on my site, they select from a dropdown which group they belong to. On the login postback, as they are logged in, I'd like to assign them to the correct SignalR group.
根据文档在这里,我可以通过以下方式加入客户端:
As per the documentation here, I can join it client-side via:
contosoChatHubProxy.server.joinGroup(groupName);
是否可以从控制器分配组?我可以像这样呼叫集线器:
Is there a way to assign the group from the controller? I can call the Hub like:
var hub = new NotificationHub()
hub.JoinGroup(selectedGroup);
,但是hub方法中的Context为空.这可能吗,还是我错误地解决了这个问题?谢谢您的任何建议.
but the Context in the hub method is null. Is this possible, or am I approaching this problem incorrectly? Thankyou for any advice.
推荐答案
您不应该新建这样的中心;您可以获取中心上下文,并通过如下外部代码将用户添加到组中:
You shouldn't new up a hub like that; you can get the hub context and add a user to a group from external code like this:
var hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
hubContext.Groups.Add(connectionId, groupName);
这篇关于来自控制器的SignalR加入组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!