SignalR异步集线器方法阻止客户端连接

问题描述 投票:1回答:1

《 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,后续调用来自同一客户端的集线器上的方法数量被阻止,直到集线器方法完成。

基于我假设方法异步时的情况,not阻止了来自同一客户端的集线器上的方法调用。

我创建了一个模板Angular / ASP.NET Core应用(dotnet新的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秒的延迟,因此客户端回复中也是如此。调试还显示,仅在第一个调用完成(任务完成)之后,第二个调用才放置在集线器上。看起来SignalR使用WebSockets传输,但是服务器按顺序处理消息,因此即使Hub方法是异步的,来自同一客户端的方法调用也会被阻止。我想念什么?还是我误会了什么?

我的实验:

    这两个GetMessage调用发生在不同的线程上,只是第二个在第一个完成后立即开始]
  • 立即处理来自其他客户端的消息(与第一个并行)
  • 如果删除返回类型(Task GetMessage(string name)),则会发生相同的事情
  • 如果我使方法同步(string GetMessage(string name)),则发生相同的事情,并且对GetMessage的两次调用发生在同一线程上(我认为是预期的)
  • ASP.NET SignalR集线器API指南-服务器(https://docs.microsoft.com/zh-cn/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server)状态以下内容:使Hub方法异步...
signalr-hub asp.net-core-signalr
1个回答
0
投票
不是完整的答案,只是一个观察。我还只是在学习使用SignalR的方法,并创建了一种仅睡眠10秒钟以模拟长时间运行的操作的方法。我阅读了以下docs article,以了解有关此内容。

SignalR每次需要处理集线器操作时(例如,当客户端连接[..]时,它都会创建一个新的Hub类实例)>
© www.soinside.com 2019 - 2024. All rights reserved.