Best practice for reconnecting SignalR 2.0 .NET client to server hub

后端 未结 5 442
忘掉有多难
忘掉有多难 2020-12-04 06:06

I\'m using SignalR 2.0 with the .NET client in a mobile application which needs to handle various types of disconnects. Sometimes the SignalR client reconnects automatically

5条回答
  •  半阙折子戏
    2020-12-04 06:37

    Setting a timer on the disconnected event to automatically attempt reconnect is the only method I am aware of.

    In javascript it is done like so:

    $.connection.hub.disconnected(function() {
       setTimeout(function() {
           $.connection.hub.start();
       }, 5000); // Restart connection after 5 seconds.
    });
    

    This is the recommended approach in the documentation:

    http://www.asp.net/signalr/overview/signalr-20/hubs-api/handling-connection-lifetime-events#clientdisconnect

提交回复
热议问题