7

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.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
user1527989
  • 139
  • 4
  • 14
  • I havent worked with SignalR that much, but im pretty sure its not one of its functionallities to check, if user is still connected to a hub. You may want to implement your own timer which will log user out after (x) minutes. – Timsen Feb 18 '13 at 13:43
  • Just feld over this link, it might be helpfull to you: http://www.dotnetcurry.com/ShowArticle.aspx?ID=826 – Timsen Feb 18 '13 at 14:12

2 Answers2

6

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.

davidfowl
  • 37,120
  • 7
  • 93
  • 103
2

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.

tugberk
  • 57,477
  • 67
  • 243
  • 335