使用HubConnection在SignalR中重新连接的正确逻辑

问题描述 投票:0回答:1

我需要将客户端应用程序(SignalR)重新连接到服务器应用程序(SignalR),直到它连接。

但它始终有ConnectionState.Reconnecting ......所以我不知道如何重新连接。

我发现这种方法qazxsw poi说我们必须创建qazxsw poi作为一种独特的工作方法......

任何线索?

我的代码是

Best practice for reconnecting SignalR 2.0 .NET client to server hub
c# connection signalr signalr.client
1个回答
0
投票

所以我发现了这个最酷的解决方案HubConnection

System.Timers.Timer connectionChecker = new System.Timers.Timer(20000);
HubConnection Connection { get; set; }

private void  ConnectionChecker_ElapsedAsync(object sender, System.Timers.ElapsedEventArgs e)
{
    if (Connection.State == ConnectionState.Disconnected)
    {
        connectionChecker.Stop();
        ForceConnectAsync().Start(); // In this method await Connection.Start();
    }
    else if (Connection.State == ConnectionState.Connecting)
    {
        // After conection lost it keeps this state ALWAYS.
        // But once server is up it still has this state.
    }
    else if (Connection.State == ConnectionState.Reconnecting)
    {
    }
    else if (Connection.State == ConnectionState.Connected)
    {
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.