【发布时间】:2019-08-09 08:17:37
【问题描述】:
ASP.NET SignalR Hubs API 指南 - 服务器 (https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server) 声明如下:
使 Hub 方法异步可避免在以下情况下阻塞连接 它使用 WebSocket 传输。当 Hub 方法执行时 同步,传输是 WebSocket,后续调用 来自同一客户端的集线器上的方法被阻止,直到集线器 方法完成。
基于我假设当方法为异步时,来自同一客户端的 Hub 上的方法调用不会被阻止。
我创建了一个模板 Angular/ASP.NET Core 应用程序 (dotnet new angular)(.Net Core 2.2.108),并添加了以下内容:
服务器:
public class ChatHub : Hub
{
public async Task<string> GetMessage(string name)
{
Console.WriteLine(DateTime.Now + " GetMessage " + name);
await Task.Delay(3000);
return "Hello " + name;
}
}
客户:
export class AppComponent {
title = 'app';
private hubConnection: signalR.HubConnection;
ngOnInit() {
this.hubConnection = new signalR.HubConnectionBuilder()
.withUrl('/chatHub')
.configureLogging(signalR.LogLevel.Debug)
.build();
this.hubConnection
.start()
.then(() => {
console.log('Connection started');
this.logMessage('a');
this.logMessage('b');
})
.catch(err => console.log('Error while starting connection: ' + err));
}
private logMessage(name) {
this.hubConnection.invoke("getMessage", name)
.then(val => console.log(new Date().toLocaleString() + ": " + val));
console.log(new Date().toLocaleString() + ": getMessage " + name + " invoked");
}
}
我在客户端得到以下输出:
[2019-08-08T13:51:09.872Z] Information: Normalizing '/chatHub' to 'https://localhost:44389/chatHub'.
Utils.js:209 [2019-08-08T13:51:09.875Z] Information: Normalizing '/chatHub' to 'https://localhost:44389/chatHub'.
Utils.js:213 [2019-08-08T13:51:09.876Z] Debug: Starting HubConnection.
Utils.js:213 [2019-08-08T13:51:09.878Z] Debug: Starting connection with transfer format 'Text'.
Utils.js:213 [2019-08-08T13:51:09.880Z] Debug: Sending negotiation request: https://localhost:44389/chatHub/negotiate.
core.js:3121 Angular is running in the development mode. Call enableProdMode() to enable the production mode.
Utils.js:213 [2019-08-08T13:51:09.930Z] Debug: Selecting transport 'WebSockets'.
Utils.js:209 [2019-08-08T13:51:09.994Z] Information: WebSocket connected to wss://localhost:44389/chatHub?id=mCisfcGtLdYEBMSIix6tvQ.
Utils.js:213 [2019-08-08T13:51:09.995Z] Debug: Sending handshake request.
Utils.js:209 [2019-08-08T13:51:09.996Z] Information: Using HubProtocol 'json'.
Utils.js:213 [2019-08-08T13:51:10.040Z] Debug: Server handshake complete.
app.component.ts:28 Connection started
app.component.ts:38 8/8/2019, 3:51:10 PM: getMessage a invoked
app.component.ts:38 8/8/2019, 3:51:10 PM: getMessage b invoked
app.component.ts:37 8/8/2019, 3:51:13 PM: Hello a
app.component.ts:37 8/8/2019, 3:51:16 PM: Hello b
在服务器上(AspNetCore 消息被剥离):
signalr-test> 2019-08-08 3:51:10 PM GetMessage a
signalr-test> 2019-08-08 3:51:13 PM GetMessage b
第二次服务器调用有 3 秒延迟,客户端回复也有 3 秒延迟。调试还显示,第二个调用仅在第一个调用完成(任务完成)后才放置在集线器上。 看起来 SignalR 使用 WebSockets 传输,但服务器按顺序处理消息,因此来自同一客户端的方法调用被阻止,即使集线器方法是异步的。
我错过了什么?还是我误会了什么?
我的实验:
- 两个 GetMessage 调用发生在不同的线程上,只是第二个在第一个完成后立即开始
- 立即处理来自其他客户端的消息(与第一个并行)
- 如果我删除返回类型 (
Task GetMessage(string name)),也会发生同样的事情 - 如果我使方法同步 (
string GetMessage(string name)),那么会发生同样的事情,而且对 GetMessage 的两次调用也会发生在同一个线程上(我认为正如预期的那样)
【问题讨论】:
-
您找到解决方案了吗?我似乎遇到了同样的问题
-
还没有,目前我只是避免它。但是根据下面 Sandro 的回答,下次我会尝试 2 个 HubConnection 对象。
标签: signalr-hub asp.net-core-signalr