How to determine server disconnection from SignalR client?
Asked Answered
G

7

29

How can a SignalR JavaScript client detect when a connection with the server is lost?

Gangster answered 1/2, 2012 at 18:17 Comment(1)
Do you use signalr with asp.net?Authenticity
T
2

The provided answers will no longer work. This is the new way to stop/disconnect connections.

myHub.stop().then(() => {
  alert('Server has disconnected');
});
Telegraphese answered 4/4, 2022 at 14:41 Comment(0)
D
33

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.

Doucet answered 3/2, 2012 at 0:48 Comment(2)
Don't use bind... just do connection.disconnectedAlidia
The proper method for this is now $.connection.hub.disconnected(callback)Often
A
13

If you are using hubs then implement the IDisconnect interface.

public class ChatHub : Hub, IDisconnect
{
    public void Disconnect()
    {
        Debug.WriteLine(Context.ConnectionId + " disconnected");
    }
}

On persistent connections you can override OnDisconnectAsync, (from the SignalR wiki at https://github.com/SignalR/SignalR/wiki/PersistentConnection )

public class MyEndPoint : PersistentConnection 
{
    protected override Task OnDisconnectAsync(string clientId) 
    {
        return Connection.Broadcast("Client " + clientId + " disconncted");   
    }
}
Autoionization answered 2/2, 2012 at 23:2 Comment(1)
This is actually very useful as an answer unrelated to the question, thank you!Lettering
K
9

This worked for me using "@aspnet/signalr": "^1.0.0" npm package

const connection = new signalr.HubConnectionBuilder()
    .withUrl('url')
    .configureLogging(signalr.LogLevel.Information)
    .build()

connection.onclose(() => {
   // close logic goes here
})
Krauss answered 3/10, 2018 at 7:15 Comment(0)
T
8

The SignalR 2.0 way of doing this is like so:

$.connection.hub.disconnected(function () {
    console.log('Connection disconnected')
});

http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-javascript-client#connectionlifetime

Tao answered 30/7, 2014 at 16:14 Comment(0)
I
6

Starting with SignalR v0.5.1 it works this way:

$.connection.hub.stateChanged(function (change) {
    if (change.newState === $.signalR.connectionState.reconnecting) {
        console.log("liveFeed is reconnecting!");
    }
    else if (change.newState === $.signalR.connectionState.connected) {
        console.log("liveFeed is connected!");
    }
});

For more details check this website:

http://weblogs.asp.net/davidfowler/archive/2012/06/10/signalr-0-5-1-released.aspx

Immediacy answered 20/10, 2012 at 11:14 Comment(0)
A
3

The below, worked for me:

var connection = $.hubConnection('signalrConnectionUrl');

connection.disconnected(function() {
    console.log('Connection disconnected');
});

I'm using version: 2.1.2

See the following link for reference: Link

Accost answered 30/1, 2015 at 9:58 Comment(0)
T
2

The provided answers will no longer work. This is the new way to stop/disconnect connections.

myHub.stop().then(() => {
  alert('Server has disconnected');
});
Telegraphese answered 4/4, 2022 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.