Overview
STOMP(面向简单文本的消息传递协议)最初是为脚本语言(例如 Ruby、Python 和 Perl)创建的,以便连接到企业消息代理。它旨在解决常用消息传递模式的最小子集。STOMP 可以通过任何可靠的双向流传输网络协议(例如 TCP 和 WebSocket)使用。尽管 STOMP 是面向文本的协议,但消息有效负载可以是文本或二进制。
STOMP (Simple Text Oriented Messaging Protocol) was originally created for scripting languages (such as Ruby, Python, and Perl) to connect to enterprise message brokers. It is designed to address a minimal subset of commonly used messaging patterns. STOMP can be used over any reliable two-way streaming network protocol, such as TCP and WebSocket. Although STOMP is a text-oriented protocol, message payloads can be either text or binary.
STOMP 是一种基于帧的协议,其帧建模在 HTTP 上。以下清单显示了 STOMP 帧的结构:
STOMP is a frame-based protocol whose frames are modeled on HTTP. The following listing shows the structure of a STOMP frame:
COMMAND header1:value1 header2:value2 Body^@
客户端可使用 SEND
或 SUBSCRIBE
命令发送或订阅表单消息,以及 destination
头,用于描述消息内容及应接收消息的人员。这样一来,便可以使用简单的发布-订阅机制,通过该机制,你可以将消息通过代理发给其他已连接的客户端,或向服务器发消息以请求执行某些工作。
Clients can use the SEND
or SUBSCRIBE
commands to send or subscribe for
messages, along with a destination
header that describes what the
message is about and who should receive it. This enables a simple
publish-subscribe mechanism that you can use to send messages through the broker
to other connected clients or to send messages to the server to request that
some work be performed.
当你使用 Spring 的 STOMP 支持时,Spring WebSocket 应用程序对客户端充当 STOMP 代理。消息会路由至 @Controller
消息处理方法或简单的内存中代理,该代理跟踪订阅并向已订阅的用户广播消息。你还可以将 Spring 配置为与专用 STOMP 代理(例如 RabbitMQ、ActiveMQ 和其他代理)配合使用,以实现消息的实际广播。在这种情况下,Spring 会维护与代理的 TCP 连接,向其传递消息,并将消息从其传递给已连接的 WebSocket 客户端。由此一来,Spring Web 应用程序就可以依赖于统一的基于 HTTP 的安全性、公共验证,以及熟悉的编程模型来处理消息。
When you use Spring’s STOMP support, the Spring WebSocket application acts
as the STOMP broker to clients. Messages are routed to @Controller
message-handling
methods or to a simple in-memory broker that keeps track of subscriptions and
broadcasts messages to subscribed users. You can also configure Spring to work
with a dedicated STOMP broker (such as RabbitMQ, ActiveMQ, and others) for the actual
broadcasting of messages. In that case, Spring maintains
TCP connections to the broker, relays messages to it, and passes messages
from it down to connected WebSocket clients. Thus, Spring web applications can
rely on unified HTTP-based security, common validation, and a familiar programming
model for message handling.
以下示例显示了订阅以接收股票报价的客户端,服务器可能会定期发出报价(例如,通过将消息通过 SimpMessagingTemplate
发送至代理的计划任务):
The following example shows a client subscribing to receive stock quotes, which
the server may emit periodically (for example, via a scheduled task that sends messages
through a SimpMessagingTemplate
to the broker):
SUBSCRIBE id:sub-1 destination:/topic/price.stock.* ^@
以下示例显示了发送交易请求的客户端,服务器可以通过 @MessageMapping
方法处理该请求:
The following example shows a client that sends a trade request, which the server
can handle through an @MessageMapping
method:
SEND destination:/queue/trade content-type:application/json content-length:44 {"action":"BUY","ticker":"MMM","shares",44}^@
执行后,服务器会广播贸易确认消息和详细信息至客户端。
After the execution, the server can broadcast a trade confirmation message and details down to the client.
目的地含义在 STOMP 规范中故意留空。它可以是任何字符串,而完全由 STOMP 服务器定义它们支持的语义和目的地的语法。但是,通常情况下,目的地是类似路径的字符串,其中 /topic/..
表示发布-订阅(一对多),而 /queue/
表示点对点(一对一)消息交换。
The meaning of a destination is intentionally left opaque in the STOMP spec. It can
be any string, and it is entirely up to STOMP servers to define the semantics and
the syntax of the destinations that they support. It is very common, however, for
destinations to be path-like strings where /topic/..
implies publish-subscribe
(one-to-many) and /queue/
implies point-to-point (one-to-one) message
exchanges.
STOMP 服务器可以使用 MESSAGE
命令向所有订阅方广播消息。以下示例显示了向已订阅的客户端发送股票报价的服务器:
STOMP servers can use the MESSAGE
command to broadcast messages to all subscribers.
The following example shows a server sending a stock quote to a subscribed client:
MESSAGE message-id:nxahklf6-1 subscription:sub-1 destination:/topic/price.stock.MMM {"ticker":"MMM","price":129.45}^@
服务器无法发送未经请求的消息。服务器必须响应特定客户端订阅才能发送所有消息,并且服务器消息的 subscription
头必须与客户端订阅的 id
头匹配。
A server cannot send unsolicited messages. All messages
from a server must be in response to a specific client subscription, and the
subscription
header of the server message must match the id
header of the
client subscription.
前面的概述旨在提供对 STOMP 协议最基本的了解。我们建议全面审阅该协议 specification。
The preceding overview is intended to provide the most basic understanding of the STOMP protocol. We recommended reviewing the protocol specification in full.