【问题标题】:SignalR async Hub method blocks client connectionSignalR 异步集线器方法阻止客户端连接
【发布时间】: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


【解决方案1】:

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

SignalR 每次需要处理 Hub 操作(例如当客户端连接 [..] 时)时都会创建 Hub 类的新实例 [..]

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

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

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

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

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

【讨论】:

  • 正如您提到的,这是一个很好的提示和可能的解决方法。如果从 hub 方法中删除 async,2 选项卡方案会发生什么情况?
  • 没有任何改变
猜你喜欢
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 2021-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多