使用 HubConnection 在 SignalR 中重新连接的正确逻辑
Correct logic to reconnect in SignalR with HubConnection
我需要将客户端应用程序 (SignalR) 重新连接到服务器应用程序 (SignalR),直到连接成功。
但它总是 ConnectionState.Reconnecting
...所以我不知道如何重新连接。
我找到了这个方法Best practice for reconnecting SignalR 2.0 .NET client to server hub
说我们必须重新创建 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)
{
}
}
所以我找到了这个最酷的解决方案Best practice for reconnecting SignalR 2.0 .NET client to server hub
private async Task<bool> ConnectToSignalRServer()
{
bool connected = false;
try
{
Connection = new HubConnection("server url");
Hub = Connection.CreateHubProxy("MyHub");
await Connection.Start();
//See @Oran Dennison's comment on @KingOfHypocrites's answer
if (Connection.State == ConnectionState.Connected)
{
connected = true;
Connection.Closed += Connection_Closed;
}
return connected;
}
catch (Exception ex)
{
Console.WriteLine("Error");
return false;
}
}
private async void Connection_Closed()
{
if(!IsFormClosed) // A global variable being set in "Form_closing" event of Form, check if form not closed explicitly to prevent a possible deadlock.
{
// specify a retry duration
TimeSpan retryDuration = TimeSpan.FromSeconds(30);
while (DateTime.UtcNow < DateTime.UtcNow.Add(retryDuration))
{
bool connected = await ConnectToSignalRServer(UserId);
if (connected)
return;
}
Console.WriteLine("Connection closed")
}
}
我需要将客户端应用程序 (SignalR) 重新连接到服务器应用程序 (SignalR),直到连接成功。
但它总是 ConnectionState.Reconnecting
...所以我不知道如何重新连接。
我找到了这个方法Best practice for reconnecting SignalR 2.0 .NET client to server hub
说我们必须重新创建 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)
{
}
}
所以我找到了这个最酷的解决方案Best practice for reconnecting SignalR 2.0 .NET client to server hub
private async Task<bool> ConnectToSignalRServer()
{
bool connected = false;
try
{
Connection = new HubConnection("server url");
Hub = Connection.CreateHubProxy("MyHub");
await Connection.Start();
//See @Oran Dennison's comment on @KingOfHypocrites's answer
if (Connection.State == ConnectionState.Connected)
{
connected = true;
Connection.Closed += Connection_Closed;
}
return connected;
}
catch (Exception ex)
{
Console.WriteLine("Error");
return false;
}
}
private async void Connection_Closed()
{
if(!IsFormClosed) // A global variable being set in "Form_closing" event of Form, check if form not closed explicitly to prevent a possible deadlock.
{
// specify a retry duration
TimeSpan retryDuration = TimeSpan.FromSeconds(30);
while (DateTime.UtcNow < DateTime.UtcNow.Add(retryDuration))
{
bool connected = await ConnectToSignalRServer(UserId);
if (connected)
return;
}
Console.WriteLine("Connection closed")
}
}