公告
财富商城
积分规则
提问
发文
2019-02-04 04:06发布
你好瞎i
The question is how can SignalR JavaScript client detect when connection with server is lost?
Thanks for any reply!
This worked for me using "@aspnet/signalr": "^1.0.0" npm package
"@aspnet/signalr": "^1.0.0"
const connection = new signalr.HubConnectionBuilder() .withUrl('url') .configureLogging(signalr.LogLevel.Information) .build() connection.onclose(() => { // close logic goes here })
A hub has a method disconnect which will allow you to add a callback when disconnection takes place:
disconnect
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.
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
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"); } }
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
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
最多设置5个标签!
This worked for me using
"@aspnet/signalr": "^1.0.0"
npm packageA hub has a method
disconnect
which will allow you to add a callback when disconnection takes place:If you aren't using hubs then the code for the disconnect method will help you out:
This shows the syntax for hooking onto the onDisconnect event of the underlying connection.
Starting with SignalR v0.5.1 it works this way:
For more details check this website:
http://weblogs.asp.net/davidfowler/archive/2012/06/10/signalr-0-5-1-released.aspx
If you are using hubs then implement the IDisconnect interface.
On persistent connections you can override OnDisconnectAsync, (from the SignalR wiki at https://github.com/SignalR/SignalR/wiki/PersistentConnection )
The below, worked for me:
I'm using version: 2.1.2
See the following link for reference: Link
The SignalR 2.0 way of doing this is like so:
http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-javascript-client#connectionlifetime