首页 > 解决方案 > SignalR 异步集线器方法阻止客户端连接

问题描述

ASP.NET SignalR 集线器 API 指南 - 服务器( https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server ) 声明如下:

使 Hub 方法异步可避免在使用 WebSocket 传输时阻塞连接。当 Hub 方法同步执行且传输为 WebSocket 时,从同一客户端对 Hub 上的方法的后续调用将被阻止,直到 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 传输,但服务器按顺序处理消息,因此来自同一客户端的方法调用阻止,即使集线器方法是异步的。

我想念什么?还是我误会了什么?

我的实验:

标签: signalr-hubasp.net-core-signalr

解决方案


不是一个完整的答案,只是一个观察。我也刚刚学习使用 SignalR 并创建了一个只休眠 10 秒来模拟长时间运行的操作的方法。我阅读了以下文档文章以了解这一点。

SignalR 每次需要处理 Hub 操作时(例如当客户端连接 [..]

所以我打开了同一页面的第二个标签。然后我在两个选项卡中调用了一次集线器方法,时间跨度为 1 秒。这正如我所期望的那样工作,该方法没有阻塞,并在 1 秒的时间跨度内将两个响应消息传递到两个选项卡。

我想这与连接有关。如果您使用相同的连接进行相同的调用,它似乎是按顺序处理的。但是如果你使用两个连接来做同样的事情,它似乎是并行处理的。

这不是一个完整的答案,因为上面的报价不完整,完整的报价如下:

SignalR 每次需要处理 Hub 操作(例如客户端连接、断开连接或对服务器进行方法调用时)都会创建 Hub 类的新实例。

也许我只是没有正确理解这一点,但目前我的观察对于我的情况来说已经足够了。不过,我仍然希望能对这种行为进行完整的解释。


推荐阅读