保持SignalR客户端连接的活动状态

3
如果我的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();
    }
}
1个回答

3
SignalR拥有自己的重新连接机制。但是经过一些重试后,状态会变为断开/关闭。断开/关闭状态意味着SignalR尝试重新连接但未能成功。因此,在此处应用重新连接以进行持续重新连接是一个好地方。
缺点是:在移动设备上,这些重新连接将使用电池。
您可以在此处查看更多详细信息:链接

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接