Azure SignalR 服务中的消息和连接Messages and connections in Azure SignalR Service

Azure SignalR 服务的计费模型基于连接数和消息数。The billing model for Azure SignalR Service is based on the number of connections and the number of messages. 本文介绍消息和连接数目的定义方式,以及它们如何影响计费。This article explains how messages and connections are defined and counted for billing.

消息格式Message formats

Azure SignalR 服务支持的格式与 ASP.NET Core SignalR 相同:JSONMessagePackAzure SignalR Service supports the same formats as ASP.NET Core SignalR: JSON and MessagePack.

消息大小Message size

Azure SignalR 服务对消息大小不施加限制。Azure SignalR Service has no size limit for messages.

大消息将拆分为小于 2 KB 的若干较小消息,并单独传输。Large messages are split into smaller messages that are no more than 2 KB each and transmitted separately. 消息拆分与组合由 SDK 进行处理,SDKs handle message splitting and assembling. 无需开发人员的干预。No developer efforts are needed.

大消息确实会对消息传送性能造成负面影响。Large messages do negatively affect messaging performance. 请尽量使用小消息,并通过测试来为每种使用方案确定最佳消息大小。Use smaller messages whenever possible, and test to determine the optimal message size for each use-case scenario.

消息如何影响计费How messages are counted for billing

计费时,我们只会统计来自 Azure SignalR 服务的出站消息。For billing, only outbound messages from Azure SignalR Service are counted. 将忽略客户端与服务器之间的 Ping 消息。Ping messages between clients and servers are ignored.

大于 2 KB 的消息算作多个大小为 2 KB 的消息。Messages larger than 2 KB are counted as multiple messages of 2 KB each. 每当每个中心出现 100 个消息时,Azure 门户中的消息计数图表就会更新。The message count chart in the Azure portal is updated every 100 messages per hub.

例如,假设你有 1 个应用程序服务器和 3 个客户端:For example, imagine you have one application server, and three clients:

应用服务器向所有连接的客户端广播 1-KB 消息,从应用服务器发送到服务的消息被视为免费入站消息。App server broadcasts a 1-KB message to all connected clients, the message from app server to the service is considered free inbound message. 只有从服务发送到每个客户端的 3 条消息按出站消息计费。Only the three messages sending from service to each of the client are billed as outbound messages.

客户端 A 将 1-KB 消息发送给另一个客户端 B,且未通过应用服务器。Client A sends a 1-KB message to another client B, without going through app server. 从客户端 A 发送到服务的消息是免费入站消息。The message from client A to service is free inbound message. 从服务发送到客户端 B 的消息按出站消息计费。The message from service to client B is billed as outbound message.

如果你有 3 个客户端和 1 个应用程序服务器。If you have three clients and one application server. 1 个客户端发送 1 条 4 KB 消息,让服务器广播到所有客户端。One client sends a 4-KB message to let the server broadcast to all clients. 计费消息计数为 8:服务向应用程序服务器发送 1 条消息,服务向客户端发送 3 条消息。The billed message count is eight: one message from the service to the application server and three messages from the service to the clients. 每条消息算作 2 条 2-KB 大小的消息。Each message is counted as two 2-KB messages.

如何统计连接数How connections are counted

Azure SignalR 服务有服务器连接和客户端连接。There are server connections and client connections with Azure SignalR Service. 默认情况下,每个应用程序服务器启动时每个中心有五个初始连接,每个客户端有一个客户端连接。By default, each application server starts with five initial connections per hub, and each client has one client connection.

Azure 门户中显示的连接计数包括服务器连接和客户端连接。The connection count shown in the Azure portal includes both server connections and client connections.

例如,假设你有 2 个应用程序服务器,并在代码中定义了 5 个中心。For example, assume that you have two application servers and you define five hubs in code. 服务器连接计数是 50:2 个应用服务器 * 5 个中心 * 每个中心的 5 个连接。The server connection count will be 50: 2 app servers * 5 hubs * 5 connections per hub.

ASP.NET SignalR 在计算服务器连接数方面有所不同。ASP.NET SignalR calculates server connections in a different way. 除了定义的中心以外,它还包括一个默认的中心。It includes one default hub in addition to hubs that you define. 默认情况下,每个应用程序服务器需要 5 个额外的初始服务器连接。By default, each application server needs five more initial server connections. 默认中心的初始连接计数与其他中心保持一致。The initial connection count for the default hub stays consistent with other hubs.

服务和应用程序服务器将保持同步连接状态,并对服务器连接进行调整,以获得更好的性能和服务稳定性。The service and the application server keep syncing connection status and making adjustment to server connections to get better performance and service stability. 因此,你可能会看到服务器连接数不时发生变化。So you might see server connection number changes from time to time.

如何统计入站/出站流量How inbound/outbound traffic is counted

发送到服务的消息是入站消息。Message sent into the service is inbound message. 从服务发出的消息是出站消息。Message sent out of the service is outbound message. 流量以字节为单位计算。Traffic is calculated in bytes.