SignalR Join Group From Controller

∥☆過路亽.° 提交于 2019-12-06 13:00:41

问题


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);

but the Context in the hub method is null. Is this possible, or am I approaching this problem incorrectly? Thankyou for any advice.


回答1:


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);


来源:https://stackoverflow.com/questions/21142889/signalr-join-group-from-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!