SignalR异步Hub方法阻止客户端连接

时间:2019-08-09 08:17:37

标签: signalr-hub asp.net-core-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,后续调用   来自同一客户端的集线器上的方法被阻止,直到集线器   方法完成。

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

我创建了一个模板Angular / ASP.NET Core应用(dotnet新的angular)(。Net Core 2.2.108),并添加了以下内容:

服务器:

let p1 = Person(name: "Name1", age: 10, designation: nil)
let p2 = Person(name: "Name1", age: 10, designation: "SE")
print(p1 == p2)

客户:

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");
  }
}

在服务器上(剥离了AspNetCore消息):

[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

第二个服务器调用中有3秒的延迟,因此客户端回复中也是如此。调试还显示,仅在第一个调用完成(任务完成)之后,第二个调用才放置在集线器上。 看起来SignalR使用WebSockets传输,但是服务器按顺序处理消息,因此即使Hub方法是异步的,来自同一客户端 的方法调用也会被阻止。

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

我的实验:

  • 这两个GetMessage调用发生在不同的线程上,只是第二个在第一个完成后立即开始
  • 来自其他客户端的消息将立即处理(与第一个并行)
  • 如果我删除返回类型(signalr-test> 2019-08-08 3:51:10 PM GetMessage a signalr-test> 2019-08-08 3:51:13 PM GetMessage b ),则会发生同样的事情
  • 如果我使方法同步(Task GetMessage(string name),则发生相同的事情,并且对GetMessage的两次调用发生在同一线程上(我认为是预期的)

1 个答案:

答案 0 :(得分:0)

没有完整的答案,只是一个观察。我还只是在学习使用SignalR的方法,并创建了一种仅睡眠10秒钟以模拟长时间运行的操作的方法。我阅读了以下docs article,以了解有关此内容的信息。

SignalR每当需要处理集线器操作时(例如,当客户端连接[..]

时)都会创建一个新的Hub类实例。

所以我打开了同一页面的第二个标签。然后,我在两个选项卡中都在1秒的时间间隔内调用了一次Hub方法。正如我所期望的那样,此方法正常运行,该方法没有阻塞,并且在1秒时间内将两条响应消息都传递到了两个选项卡。

我想这与连接有关。如果您使用相同的连接进行相同的呼叫,则似乎将按顺序处理该呼叫。但是,如果您使用两个连接执行相同的操作,则它似乎是并行处理的。

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

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

也许我只是不正确地理解了这一点,但就目前而言,我的观察足以满足我的情况。不过,我仍然希望对此行为进行完整的解释。