如何仅通过一个连接并行发送多条 SignalR 消息?
我有以下集线器方法:
public async Task Test()
{
Console.WriteLine($"Start: {DateTime.Now}");
await Task.Run(() => Task.Delay(1000));
Console.WriteLine($"Stop: {DateTime.Now}");
}
在我的客户端中,我有以下连接:
private connection: HubConnection = new HubConnectionBuilder()
.withUrl(this.url)
.configureLogging(LogLevel.Information)
.withAutomaticReconnect([1000, 2000, 3000, 4000, 5000])
.build();
我这样调用集线器方法:
async test(): Promise<void> {
try {
console.log('start: ' + new Date().toString());
await this.connection.invoke('Test');
console.log('stop: ' + new Date().toString());
} catch (err) {
console.error(err);
}
}
当我多次调用客户端中的测试方法时,集线器会一个接一个地接收消息,而不是一次接收所有消息。
客户端日志:
start: Mon Nov 29 2021 17:27:35 GMT+0100 (3)
stop: Mon Nov 29 2021 17:27:36 GMT+0100
stop: Mon Nov 29 2021 17:27:37 GMT+0100
stop: Mon Nov 29 2021 17:27:38 GMT+0100
服务器日志:
Start: 29.11.2021 17:27:35
Stop: 29.11.2021 17:27:36
Start: 29.11.2021 17:27:36
Stop: 29.11.2021 17:27:37
Start: 29.11.2021 17:27:37
Stop: 29.11.2021 17:27:38
最佳答案
为了允许每个客户端进行多个并行调用,您需要设置 MaximumParallelInvocationsPerClient属性(property),像这样:
builder.Services.AddSignalR(options => options.MaximumParallelInvocationsPerClient = 10);
关于c# - 使用一个连接发送并发 SignalR 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70157999/