I have a SignalR hub that receives messages from clients that call a custom "login" method. That method adds the connection to a group and then sends a message back to the client.
The hub receives the message from the client, but the client never receives the response message that is sent by the hub using SendAsync().
.NET 5
Microsoft.AspNetCore.SignalR.Client 5.0.9
Here's the Server Code:
public class PushSignalBroadcastHub : Hub
{
private IConfiguration Config { get; set; }
public PushSignalBroadcastHub( IConfiguration configuration )
{
Config = configuration;
}
public override Task OnConnectedAsync()
{
// wait until login to add the connection to the table
return base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync( Exception exception )
{
await base.OnDisconnectedAsync( exception );
}
public async Task<string> Login( string accountNumber )
{
await Groups.AddToGroupAsync( Context.ConnectionId, accountNumber );
await Clients.Caller.SendAsync( "login" );
return "ok";
}
}
Here is my client code:
private async Task StartConnection()
{
_connection = new HubConnectionBuilder()
.WithUrl( "wss://localhost:44328/pushsignalhub", options =>
{ options.SkipNegotiation = true; options.Transports = HttpTransportType.WebSockets; } )
.Build();
_connection.On( "login", () =>
{
Console.WriteLine( "done" );
} );
try
{
await _connection.StartAsync();
}
catch ( Exception e )
{
Console.WriteLine( e.Message );
}
await _connection.InvokeAsync( "login", "test" );
}
With the above code, the hub receives the login with "test" as its parameter. But I never receive the message the hub sends.
Is there anything I'm missing, or not getting right?
Thanks.