Websocket 保持SignalR客户端连接处于活动状态

Websocket 保持SignalR客户端连接处于活动状态,websocket,signalr,signalr.client,Websocket,Signalr,Signalr.client,如果我的SignalR客户端连接中断,我希望客户端尝试重新连接。这是实现这一点的好模式吗?我指的是通过重新启动连接来处理信号机连接上的关闭事件 public class OnPremiseWebHubClient { private HubConnection _hubConnection; private IHubProxy _hubProxy; private OnPremiseWebHubClient() { } static OnPremiseWebHu

如果我的SignalR客户端连接中断,我希望客户端尝试重新连接。这是实现这一点的好模式吗?我指的是通过重新启动连接来处理信号机连接上的关闭事件

public class OnPremiseWebHubClient
{
    private HubConnection _hubConnection;
    private IHubProxy _hubProxy;

    private OnPremiseWebHubClient() { }
    static OnPremiseWebHubClient() { }
    private static readonly OnPremiseWebHubClient _instance = new OnPremiseWebHubClient();
    public static OnPremiseWebHubClient Instance { get { return _instance; } }

    public async Task Start()
    {
        _hubConnection = new HubConnection("http://OnPremiseWeb/");
        _hubProxy = _hubConnection.CreateHubProxy("OnPremiseHub");

        // IS THIS A GOOD PATTERN FOR KEEPING THE CONNECTION ALIVE?
        _hubConnection.Closed += async () =>
        {
            // reconnect if we close
            await _hubConnection.Start();
        };

        await _hubConnection.Start();
    }
}

信号机有自己的重新连接机制。但在一些重试之后,状态将变为断开连接/关闭。断开/关闭状态表示信号器尝试重新连接,但无法实现。因此,这是一个很好的应用重新连接的地方

有一个缺点:在移动设备上,重新连接将使用电池

您可以查看更多详细信息