How to determine server disconnection from SignalR client?

前端 未结 6 709
Happy的楠姐
Happy的楠姐 2020-12-28 14:45

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

Thanks for any reply!

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-28 14:52

    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.

提交回复
热议问题