当我收到一个信号核心reconnected从我的HubConnection中获得的事件时,我希望将连接到轮毂状态,但通常会连接连接! 这似乎有些反直观!
我已启用了autoReconnect
我可能必须等待(循环),直到状态=连接,因为我需要启动东西!
或有更好的方法?
您是正确的,因为事件仅触发一次,所以您必须循环直到状态真正改变。
HubConnection.Reconnected += async (connectionId) =>
{
Console.WriteLine($"Reconnected with ConnectionId: {connectionId}");
// Loop until the state changes to Connected
while (HubConnection.State != HubConnectionState.Connected)
{
Console.WriteLine($"Waiting for connection to stabilize... Current state: {HubConnection.State}");
await Task.Delay(500); // Wait for 500ms before checking again
}
Console.WriteLine("Connection is fully established.");
};