【发布时间】: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”作为参数的登录。但我从未收到集线器发送的消息。
我有什么遗漏或没有做对吗?
谢谢。
【问题讨论】:
-
我认为
InvokeAsync的第一个参数可能区分大小写 -
除非调用成功。我的登录方法中的断点命中。 _connection.On() 中的方法失败了。
-
在不使用 SSL 的情况下尝试一下:
ws://localhost。您可能会在后台遇到一些 SSL 验证错误。 -
添加一个传输记录器,然后逐步执行该方法。输出窗口会告诉你原因。
标签: c# asp.net-core websocket signalr