Angular with signalR: 到同一路由的多个集线器连接

Angular with signalR: Multiple hubconnections to the same route

我的客户端代码如下。我有一个信号路由和集线器连接名称。我能够连接到集线器并获取事件。现在我需要为同一路线连接到另一个集线器以获取更多事件。

  this.hubConnection = this.window.$.hubConnection();
    this.hubConnection.url = this.apiUrl  + "api/signalr";
    this.hubProxy = this.hubConnection.createHubProxy("Event1Hub");
    this.hubProxy = this.hubConnection.createHubProxy("Event2Hub"); // When I add this line it is overriding first hub and only connecting to Event2Hub.

我正在使用 "signalr": "2.3.0",

您正在为 Event1Hub 创建一个代理,然后使用 Event2Hub 的代理重新分配该对象实例。如果您想与两个集线器进行通信,则必须初始化单独的代理对象才能使其正常工作。

this.hubConnection = this.window.$.hubConnection();
this.hubConnection.url = this.apiUrl  + "api/signalr";
this.hubEvent1Proxy = this.hubConnection.createHubProxy("Event1Hub");
this.hubEvent2Proxy = this.hubConnection.createHubProxy("Event2Hub");

这样 hubEvent1Proxy 将提供 Event1Hub 通信,而 hubEvent2Proxy 将提供 Event2Hub 通信。