when I receive a SignalR core Reconnected event from my hubconnection, I expect the hub state to be Connected, but its often Connecting! This seems somewhat counter intuitive!
I have autoreconnect enabled
I'm probably going to have to wait (in a loop) until state=Connected as I need to kick stuff off!
Or is there a better approach?
when I receive a SignalR core Reconnected event from my hubconnection, I expect the hub state to be Connected, but its often Connecting! This seems somewhat counter intuitive!
I have autoreconnect enabled
I'm probably going to have to wait (in a loop) until state=Connected as I need to kick stuff off!
Or is there a better approach?
Share Improve this question edited Feb 8 at 19:40 fpdave100 asked Feb 7 at 12:40 fpdave100fpdave100 616 bronze badges 2- Use the Reconnected event to kick stuff off? – Brennan Commented Feb 7 at 17:01
- Sorry, important typo - it is the Reconnected event I'm handling, and finding that state=connecting! Very sorry – fpdave100 Commented Feb 8 at 19:41
1 Answer
Reset to default 0You are right, since the event only trigger once, you have to loop until state really changed.
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.");
};