Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disconnect client from server side signalr

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     
}

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:

The connection id is in the incorrect format.

like image 522
Haddar Macdasi Avatar asked Apr 08 '13 21:04

Haddar Macdasi


People also ask

Is SignalR bidirectional?

ASP.NET SignalR is a new library for ASP.NET developers that makes developing real-time web functionality easy. SignalR allows bi-directional communication between server and client. Servers can now push content to connected clients instantly as it becomes available.

How do I set SignalR connection timeout?

Timeout configuration for SignalR can be set in Application_Start method of Global class in Global. asax. cs file. // Wait a maximum of 30 minutes after a transport connection is lost // before raising the Disconnected event to terminate the SignalR connection.


1 Answers

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.
like image 179
halter73 Avatar answered Sep 23 '22 05:09

halter73