user1527989
user1527989

Reputation: 139

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.

Upvotes: 7

Views: 6872

Answers (2)

davidfowl
davidfowl

Reputation: 38864

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.

Upvotes: 6

tugberk
tugberk

Reputation: 58494

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.

Upvotes: 2

Related Questions