How to determine server disconnection from SignalR client?

Alexandr picture Alexandr · Feb 1, 2012 · Viewed 26.5k times · Source

The question is how can SignalR JavaScript client detect when connection with server is lost?

Thanks for any reply!

Answer

John Rayner picture John Rayner · Feb 3, 2012

A hub has a method disconnect which will allow you to add a callback when disconnection takes place:

myHub.disconnect(function() {
  alert('Server has disconnected');
});

If you aren't using hubs then the code for the disconnect method will help you out:

$(connection).bind("onDisconnect", function (e, data) {
  callback.call(connection);
});

This shows the syntax for hooking onto the onDisconnect event of the underlying connection.