断开客户端与服务器端信号器

Had*_*asi 13 asp.net-mvc-4 signalr

我正在使用带有表单身份验证的MVC4 C#Web应用程序的SignalR 1.我在JavaScript的布局页面中有一个代码:

$(documnet).ready(function(){
       connect to hub code ...
})
Run Code Online (Sandbox Code Playgroud)

我想断开用户从集线器断开连接并在他登录后再次开始连接并验证确定.我想从我的帐户控制器和方法中的服务器端执行此操作:

public ActionResult LogOn(LoginModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        if (System.Web.Security.Membership.ValidateUser(model.UserName, model.Password))
        {
            FormsAuthentication.SetAuthCookie(model.UserName, false);
            ....here , disconnect from hub
            ....to make the user reconnect     
}
Run Code Online (Sandbox Code Playgroud)

我想这样做的原因是,如果用户在登录后更改为经过身份验证并且连接仍然存在,则SignalR会抛出错误.错误是:

连接ID格式不正确.

hal*_*r73 25

您无法从服务器停止和启动SignalR连接.你需要打电话

$.connection.hub.stop(); //on the client before the user attempts to log on and then call 
$.connection.hub.start(); //after the log on attempt has completed.
Run Code Online (Sandbox Code Playgroud)

  • 如果员工刚刚被解雇并且需要撤销其访问权限,会发生什么情况?我需要能够断开任何打开的 signalR 会话。必须有办法。 (3认同)

Uto*_*Ltd 12

您可以做的一种方法是在客户端上编写一个断开事件,服务器可以通过SignalR调用.也许有点像这样:

myHub.client.serverOrderedDisconnect = function (value) {
    $.connection.hub.stop();
};
Run Code Online (Sandbox Code Playgroud)

然后,在服务器上,这样的事情:

Clients.Client(Context.ConnectionId).serverOrderedDisconnect();
Run Code Online (Sandbox Code Playgroud)


Лоп*_*ная 6

如果有人仍在寻找解决方案(SignalR 版本 2.4.1):

GlobalHost.DependencyResolver.Resolve<ITransportHeartbeat>().GetConnections().First(c => c.ConnectionId == "YourId").Disconnect();
Run Code Online (Sandbox Code Playgroud)

  • 在aspnetcore中如何做到这一点? (2认同)