决定要连接到哪个 SignalR Hub

Decide which SignalR Hub to connect to

我对使用 SignalR 决定连接或不连接哪个 Hub 的规则感到困惑。

在javascript中,如果我写$.connection.hub.start(),并说我有两个集线器(集线器1和集线器2),那么连接哪个集线器?或者它们都已连接?

两者都会连接。如果您查看文档:http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server#multiplehubs

  • You can define multiple Hub classes in an application. When you do that, the connection is shared but groups are separate:

All clients will use the same URL to establish a SignalR connection with your service ("/signalr" or your custom URL if you specified one), and that connection is used for all Hubs defined by the service.

There is no performance difference for multiple Hubs compared to defining all Hub functionality in a single class.

  • All Hubs get the same HTTP request information.

Since all Hubs share the same connection, the only HTTP request information that the server gets is what comes in the original HTTP request that establishes the SignalR connection. If you use the connection request to pass information from the client to the server by specifying a query string, you can't provide different query strings to different Hubs. All Hubs will receive the same information.

我从客户端 js 指南中找到了以下内容。 “集线器代理上客户端方法的存在告诉 SignalR 触发 OnConnected 事件”

Note: Normally you register event handlers before calling the start method to establish the connection. If you want to register some event handlers after establishing the connection, you can do that, but you must register at least one of your event handler(s) before calling the start method. One reason for this is that there can be many Hubs in an application, but you wouldn't want to trigger the OnConnected event on every Hub if you are only going to use to one of them. When the connection is established, the presence of a client method on a Hub's proxy is what tells SignalR to trigger the OnConnected event. If you don't register any event handlers before calling the start method, you will be able to invoke methods on the Hub, but the Hub's OnConnected method won't be called and no client methods will be invoked from the server.