Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR disconnect not being called on internet disconnection/re-connection

I am using SignalR in my app.I have an app which depends to a very great degree on OnDisconnected() being called correctly. And it is called correctly under the following circumstances:

 public Task OnDisconnected()
 {
     try
     {
        DeleteUser(Context.ConnectionId);
        return null;
     }
     catch (Exception ex)
     {
        return null;
     }
}
  1. The user refreshes the page
  2. The user navigates to a new page
  3. The user closes the browser

However, it is not called if the network connection suddenly drops. For instance, if I unplug the network cable on the client machine, or disable the client's wireless network, or unplug the router, OnDisconnected() will never get called, even after a several minute wait.

like image 586
user1527989 Avatar asked Feb 18 '13 13:02

user1527989


People also ask

How do you simulate SignalR reconnect?

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.

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.

What is long polling in SignalR?

Long polling is about doing over the Web the same things you do in a desktop scenario. With long polling, the client places the request and the server doesn't reply until it has information to return. The Web client keeps a pending connection that's closed only when some valid response can be returned.


2 Answers

It will raise disconnected but not immediately. There's configurable a threshold (30 seconds by default) that SignalR will wait (after the underlying tcp connection has gone away and this isn't immediate either) before it considers a client disconnected. If the connection drops and reconnects before the configured timeout then it won't raise OnDisconnected.

If you're never seeing it being raised in some scenario after waiting for a while then it might be a bug. SignalR 1.0 was released today so I'd encourage you to try that as well and see if you still see the problem.

like image 97
davidfowl Avatar answered Oct 21 '22 05:10

davidfowl


This may not be the right answer but this is what I know:

You won't be able to see the OnDisconnected event fired suddenly when the connection is dropped because SignalR doesn't track it down (it pools for connection with a background task to see if connection is dead within a certain interval). When you close the browser, I'm guessing that SignalR sends a request to server to signal the disconnect event. That's why you suddenly see the event fired.

However, ASP.NET 4.5 has a CancellationToken property called ClientDisconnectedToken for HttpContext.Response which is signaled when TCP connection is dropped. This only works under IIS 8.0 as far as I know and I'm not sure if SignalR works with this under .NET 4.5 ASP.NET host.

like image 24
tugberk Avatar answered Oct 21 '22 03:10

tugberk