3

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.

Brad Y.
  • 161
  • 2
  • 14
  • I think the first parameter to `InvokeAsync` may be case sensitive – Menachem Hornbacher Aug 16 '21 at 19:35
  • also I think the method name is `invoke`: https://learn.microsoft.com/en-us/javascript/api/%40aspnet/signalr/hubconnection?view=signalr-js-latest#invoke and https://learn.microsoft.com/en-us/javascript/api/@microsoft/signalr/hubconnection?view=signalr-js-latest#invoke-string--any--- – Menachem Hornbacher Aug 16 '21 at 19:37
  • Except that the invoke succeeds. My breakpoints in my Login method hit. It's the method in _connection.On() that fails. – Brad Y. Aug 16 '21 at 19:44
  • 1
    Try it without using SSL : `ws://localhost`. You might be getting some SSL verification errors in the background. – Eldar Aug 16 '21 at 19:47
  • Add a transport logger then step through the method. The output window will show you why. – GH DevOps Aug 16 '21 at 19:48
  • It shows I'm receiving something from the server, as I show here: `dbug: Microsoft.AspNetCore.SignalR.Client.HubConnection[6] Sending InvocationMessage message '3' completed. dbug: Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport[13] Received message from application. Payload size: 68.` – Brad Y. Aug 16 '21 at 20:08
  • Is there a way to capture ANY message from SignalR, regardless of method? – Brad Y. Aug 16 '21 at 20:08
  • Hu, possible that I have the case wrong, been a few months since I used SignalR. Oh and this is the .NET client :facepalm: – Menachem Hornbacher Aug 16 '21 at 21:23
  • I've noticed that I'm not creating the client code the way a lot of the examples online do it. They create a new HubConnection, then create a proxy and code it like that. I can't do that. When I create a HubConnection, there's no CreateProxy() method. So, I do it this way, which I read from an article. I guess I'm using a different package, but it's the one everyone seems to recommend. – Brad Y. Aug 17 '21 at 11:38
  • The logging shows me receiving all the incoming data, but no .On Actions are getting called. – Brad Y. Aug 17 '21 at 11:51
  • Turns out @Eldar was right. I had to turn off SSL. It must have been CORS or something getting in the way. Now I can test and move on. Thanks everyone! – Brad Y. Aug 17 '21 at 13:23

1 Answers1

0

The problem turned out to be with CORS. I had to properly configure CORS and it worked.

Brad Y.
  • 161
  • 2
  • 14