Azure SignalR 服务内部Azure SignalR Service internals

Azure SignalR 服务构建于 ASP.NET Core SignalR 框架的基础之上。Azure SignalR Service is built on top of ASP.NET Core SignalR framework. 它还通过在 ASP.NET Core 框架之上重新实现 ASP.NET SignalR 的数据协议来支持 ASP.NET SignalR。It also supports ASP.NET SignalR by reimplementing ASP.NET SignalR's data protocol on top of the ASP.NET Core framework.

只需更改少量的几行代码,即可轻松迁移本地 ASP.NET Core SignalR 应用程序或 ASP.NET SignalR 应用程序以使用 SignalR 服务。You can easily migrate a local ASP.NET Core SignalR application or ASP.NET SignalR application to work with SignalR Service, with a few lines of code change.

下图描绘了在应用程序服务器上使用 SignalR 服务时的典型体系结构。The diagram below describes the typical architecture when you use the SignalR Service with your application server.

本文还会介绍该服务与自承载式 ASP.NET Core SignalR 应用程序的差异。The differences from self-hosted ASP.NET Core SignalR application are discussed as well.


服务器连接Server connections

自承载式 ASP.NET Core SignalR 应用程序服务器侦听客户端并直连到客户端。Self-hosted ASP.NET Core SignalR application server listens to and connects clients directly.

使用 SignalR 服务时,应用程序服务器不再接受持久性客户端连接,而是:With SignalR Service, the application server is no longer accepting persistent client connections, instead:

  1. 由 Azure SignalR 服务 SDK 为每个中心公开一个 negotiate 终结点。A negotiate endpoint is exposed by Azure SignalR Service SDK for each hub.
  2. 此终结点响应客户端的协商请求,并将客户端重定向到 SignalR 服务。This endpoint will respond to client's negotiation requests and redirect clients to SignalR Service.
  3. 客户端最终连接到 SignalR 服务。Eventually, clients will be connected to SignalR Service.

有关详细信息,请参阅客户端连接For more information, see Client connections.

启动应用程序服务器后:Once the application server is started,

  • 对于 ASP.NET Core SignalR,Azure SignalR 服务 SDK 将为 SignalR 服务开放每个中心的 5 个 WebSocket 连接。For ASP.NET Core SignalR, Azure SignalR Service SDK opens 5 WebSocket connections per hub to SignalR Service.
  • 对于 ASP.NET SignalR,Azure SignalR 服务 SDK 将为 SignalR 服务开放每个中心的 5 个 WebSocket 连接,并为每个应用程序开放一个 WebSocket 连接。For ASP.NET SignalR, Azure SignalR Service SDK opens 5 WebSocket connections per hub to SignalR Service, and one per application WebSocket connection.

5 个 WebSocket 连接是默认值,可在配置中更改。5 WebSocket connections is the default value that can be changed in configuration.

发往和发自客户端的消息将在这些连接中多路复用。Messages to and from clients will be multiplexed into these connections.

始终与 SignalR 服务保持这些连接。These connections will remain connected to the SignalR Service all the time. 如果网络问题导致服务器连接断开:If a server connection is disconnected for network issue,

客户端连接Client connections

使用 SignalR 服务时,客户端将连接到 SignalR 服务而不是应用程序服务器。When you use the SignalR Service, clients connect to SignalR Service instead of application server. 在客户端与 SignalR 服务之间建立持久性连接需要执行两个步骤。There are two steps to establish persistent connections between the client and the SignalR Service.

  1. 客户端向应用程序服务器发送协商请求。Client sends a negotiate request to the application server. 应用程序服务器使用 Azure SignalR 服务 SDK 返回包含 SignalR 服务 URL 和访问令牌的重定向响应。With Azure SignalR Service SDK, application server returns a redirect response with SignalR Service's URL and access token.
  • 对于 ASP.NET Core SignalR,典型的重定向响应如下所示:For ASP.NET Core SignalR, a typical redirect response looks like:
        "accessToken":"<a typical JWT token>"
  • 对于 ASP.NET SignalR,典型的重定向响应如下所示:For ASP.NET SignalR, a typical redirect response looks like:
        "AccessToken":"<a typical JWT token>"
  1. 收到重定向响应后,客户端使用新的 URL 和访问令牌启动连接到 SignalR 服务的一般过程。After receiving the redirect response, client uses the new URL and access token to start the normal process to connect to SignalR Service.

详细了解 ASP.NET Core SignalR 的传输协议Learn more about ASP.NET Core SignalR's transport protocols.

客户端与服务器之间的数据传输Data transmit between client and server

客户端连接到 SignalR 服务后,服务运行时将查找服务器连接,以便为此客户端提供服务When a client is connected to the SignalR Service, service runtime will find a server connection to serve this client

  • 此步骤只发生一次,它是客户端与服务器连接之间的一对一映射。This step happens only once, and is a one-to-one mapping between the client and server connections.
  • 在客户端或服务器断开连接之前,SignalR 服务中会一直保持这种映射。The mapping is maintained in SignalR Service until the client or server disconnects.

此时,应用程序服务器会收到一个事件,其中包含来自新客户端的信息。At this point, the application server receives an event with information from the new client. 在应用程序服务器中与客户端建立逻辑连接。A logical connection to the client is created in the application server. 通过 SignalR 服务建立从客户端到应用程序服务器的数据通道。The data channel is established from client to application server, via SignalR Service.

SignalR 服务将客户端中的数据传输到配对的应用程序服务器。SignalR Service transmits data from the client to the pairing application server. 应用程序服务器中的数据将发送到映射的客户端。And data from the application server will be sent to the mapped clients.

SignalR 服务不保存或存储客户数据,接收的所有客户数据将实时传输到目标服务器或客户端。SignalR Service does not save or store customer data, all customer data received is transmitted to target server or clients in real-time.

可以看出,Azure SignalR 服务本质上是应用程序服务器与客户端之间的逻辑传输层。As you can see, the Azure SignalR Service is essentially a logical transport layer between application server and clients. 所有持久性连接将卸载到 SignalR 服务。All persistent connections are offloaded to SignalR Service. 应用程序服务器只需处理中心类中的业务逻辑,而无需担心客户端连接。Application server only needs to handle the business logic in hub class, without worrying about client connections.