【问题标题】:SignalR hub receives messages, but my client never receives messages sent outSignalR 集线器接收消息,但我的客户端从未收到发出的消息
【发布时间】:2021-10-18 19:47:07
【问题描述】:

我有一个 SignalR 集线器,它接收来自调用自定义“登录”方法的客户端的消息。该方法将连接添加到组,然后将消息发送回客户端。

集线器从客户端接收消息,但客户端从未收到集线器使用 SendAsync() 发送的响应消息。

.NET 5
Microsoft.AspNetCore.SignalR.Client 5.0.9

这是服务器代码:

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";
    }
}

这是我的客户端代码:

    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" );
    }

使用上面的代码,集线器接收带有“test”作为参数的登录。但我从未收到集线器发送的消息。

我有什么遗漏或没有做对吗?

谢谢。

【问题讨论】:

标签: c# asp.net-core websocket signalr


【解决方案1】:

问题出在 CORS 上。我必须正确配置 CORS 并且它起作用了。

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 2020-08-13
    • 2016-08-25
    • 1970-01-01
    • 2016-04-29
    相关资源
    最近更新 更多