Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR: client disconnection

How does the SignalR handle client disconnection? Am I right if I state the following?

  • SignalR will detect browser page close/refresh via Javascript event handling and will send appropriate packet to server (through the persisting connection);
  • SignalR will NOT detect browser close/network failure (probably only by timeout).

I aim the long-polling transport.

I'm aware of this question but would like to make it a bit clear for me.

like image 427
pavel.baravik Avatar asked Mar 20 '12 15:03

pavel.baravik


People also ask

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.

How do I check if my SignalR is reconnecting?

To test reconnect after the server goes down use iisreset. To simulate client connection dropping (good luck) pull the network cable :) Pulling the network cable won't accurately simulate a client connection dropping when you're using Azure SignalR Service.

Does SignalR require sticky session?

Sticky Sessions SignalR requires that all HTTP requests for a specific connection be handled by the same server process. When SignalR is running on a server farm (multiple servers), "sticky sessions" must be used. "Sticky sessions" are also called session affinity by some load balancers.


2 Answers

If a user refreshes the page, that is treated as a new connection. You are correct that the disconnect is based on a timeout.

You can handle the Connect/Reconnect and Disconnect events in a Hub by implementing SignalR.Hubs.IConnected and SignalR.Hubs.IDisconnect.

The above referred to SignalR 0.5.x.

From the official documentation (currently for v1.1.3):

public class ContosoChatHub : Hub
{
    public override Task OnConnected()
    {
        // Add your own code here.
        // For example: in a chat application, record the association between
        // the current connection ID and user name, and mark the user as online.
        // After the code in this method completes, the client is informed that
        // the connection is established; for example, in a JavaScript client,
        // the start().done callback is executed.
        return base.OnConnected();
    }

    public override Task OnDisconnected()
    {
        // Add your own code here.
        // For example: in a chat application, mark the user as offline, 
        // delete the association between the current connection id and user name.
        return base.OnDisconnected();
    }

    public override Task OnReconnected()
    {
        // Add your own code here.
        // For example: in a chat application, you might have marked the
        // user as offline after a period of inactivity; in that case 
        // mark the user as online again.
        return base.OnReconnected();
    }
}
like image 194
drch Avatar answered Oct 01 '22 10:10

drch


In SignalR 1.0, the SignalR.Hubs.IConnected and SignalR.Hubs.IDisconnect are no longer implemented, and now it's just an override on the hub itself:

public class Chat : Hub
{
    public override Task OnConnected()
    {
        return base.OnConnected();
    }

    public override Task OnDisconnected()
    {
        return base.OnDisconnected();
    }

    public override Task OnReconnected()
    {
        return base.OnReconnected();
    }
}
like image 38
Richard Pursehouse Avatar answered Oct 01 '22 10:10

Richard Pursehouse