断开客户端与服务器端信号器的连接 [英] disconnect client from server side signalr
问题描述
我将SignalR 1与具有表单身份验证的MVC4 C#Web应用程序一起使用. 我的JavaScript布局页面中有一个代码:
I'm using SignalR 1 with MVC4 C# web application with form authentication. I have a code in my layout page in JavaScript :
$(documnet).ready(function(){
connect to hub code ...
})
我要断开用户与集线器的连接,并在他登录并确认确定后再次开始连接. 我想从服务器上在我的帐户控制器和方法中完成此操作:
I want to disconnect a user form the hub and start connect again after he does a login and validate ok. I want to do it from server side inside my account controller and method :
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
}
我要这样做的原因是,如果用户在登录后更改为Authenticated并且连接仍然存在,则SignalR会引发错误.错误是:
The reason I want to do it is because SignalR throws an error if user changed to authenticated after login and the connection remains . The error is:
连接ID的格式错误.
The connection id is in the incorrect format.
推荐答案
您无法从服务器停止和启动SignalR连接.您需要致电
You cannot stop and start SignalR connections from the server. You will need to call
$.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.
这篇关于断开客户端与服务器端信号器的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!