OnConnectedAsync missing some of the connections
See original GitHub issueI’m creating 10 clients in a loop. Almost all the time, a few of these clients don’t trigger the OnConnectedAsync method when they connect to the hub. These clients don’t throw any exceptions when they start a connection using the connection.StartAsync() or when the connection.On handler is registered. Which makes it hard to diagnose any problems.
Fiddler traffic displays 10 client/negotiate, 10 client/?hub, 10 /message/negotiate requests. But I see 9 OnConnectedAsync calls in the console.
This is the fiddler traffic:
This is the Hub’s OnConnectedAsync method:
public override async Task OnConnectedAsync()
{
Console.WriteLine("@@@ OnConnectedAsync is executed");
if (Context?.User?.Identity != null)
{
var playerId = ((System.Security.Claims.ClaimsIdentity)Context.User.Identity).Claims
.First(p => p.Type == "PlayerId")
.Value;
//some logic
}
await base.OnConnectedAsync().ConfigureAwait(false);
}
This is the client code:
for (var i = 1; i <= 10; i++)
{
var token = await program.GetToken(i);
await program.CreateClient(i, token);
}
private async Task CreateClient(int playerId, string token)
{
var connection = new HubConnectionBuilder()
.WithUrl(_configurationRoot["SignalRHubUrl"],
options => options.Headers = new Dictionary<string, string>
{
{
"Authorization", $"bearer {token}"
}
})
.Build();
await connection.StartAsync();
connection.On<string>("send", async (m) =>
{
Console.WriteLine($"Message received: {m}");
await connection.StopAsync();
});
Console.WriteLine($"Player {playerId} connected.");
}
These are the console logs that display OnConnectedAsync’s:
Now listening on: http://localhost:51234 Application started. Press Ctrl+C to shut down. info: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[3] HttpConnection Started. info: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[3] HttpConnection Started. info: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[3] HttpConnection Started. info: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[3] HttpConnection Started. info: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[3] HttpConnection Started. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 264.7313ms 200 application/json info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 8.4074ms 200 application/json info: Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info@@@ OnConnectedAsync is executed : Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 7.6316ms 200 application/json info: Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. @@@ OnConnectedAsync is executed info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 13.7569ms 200 application/json info: Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. @@@ OnConnectedAsync is executed info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 8.4681ms 200 application/json info: Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. @@@ OnConnectedAsync is executed info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 6.1647ms 200 application/json info@@@ OnConnectedAsync is executed : Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 6.8534ms 200 application/json info: Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. @@@ OnConnectedAsync is executed info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 6.5252ms 200 application/json info: Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. @@@ OnConnectedAsync is executed info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 7.1773ms 200 application/json info@@@ OnConnectedAsync is executed : Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 6.7461ms 200 application/json info: Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. @@@ OnConnectedAsync is executed
ConnectionId of player 2 is missing:
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (5 by maintainers)
Top GitHub Comments
@yigitgursoy We found the root cause of this issue. It is an edge case and only happens on
Free
tier. We have fixed it and will update here once it is deployed.Unrelated:
You should be using the access token provider to set the bearer token.
You don’t need to cast this:
It’s already a ClaimsIdentity.