此版本仍在开发中,尚未被视为稳定版。如需最新稳定版本,请使用 Spring Framework 7.0.6spring-doc.cadn.net.cn

启用 STOMP

STOMP over WebSocket 的支持包含在 spring-messagingspring-websocket 模块中。一旦你添加了这些依赖项,就可以通过 WebSocket 公开一个 STOMP 端点,如下例所示:spring-doc.cadn.net.cn

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {

	@Override
	public void registerStompEndpoints(StompEndpointRegistry registry) {
		// /portfolio is the HTTP URL for the endpoint to which a WebSocket (or SockJS)
		// client needs to connect for the WebSocket handshake
		registry.addEndpoint("/portfolio");
	}

	@Override
	public void configureMessageBroker(MessageBrokerRegistry config) {
		// STOMP messages whose destination header begins with /app are routed to
		// @MessageMapping methods in @Controller classes
		config.setApplicationDestinationPrefixes("/app");
		// Use the built-in message broker for subscriptions and broadcasting and
		// route messages whose destination header begins with /topic or /queue to the broker
		config.enableSimpleBroker("/topic", "/queue");
	}
}
@Configuration
@EnableWebSocketMessageBroker
class WebSocketConfiguration : WebSocketMessageBrokerConfigurer {

	override fun registerStompEndpoints(registry: StompEndpointRegistry) {
		// /portfolio is the HTTP URL for the endpoint to which a WebSocket (or SockJS)
		// client needs to connect for the WebSocket handshake
		registry.addEndpoint("/portfolio")
	}

	override fun configureMessageBroker(config: MessageBrokerRegistry) {
		// STOMP messages whose destination header begins with /app are routed to
		// @MessageMapping methods in @Controller classes
		config.setApplicationDestinationPrefixes("/app")
		// Use the built-in message broker for subscriptions and broadcasting and
		// route messages whose destination header begins with /topic or /queue to the broker
		config.enableSimpleBroker("/topic", "/queue")
	}
}
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:websocket="http://www.springframework.org/schema/websocket"
	   xsi:schemaLocation="
			http://www.springframework.org/schema/beans
			https://www.springframework.org/schema/beans/spring-beans.xsd
			http://www.springframework.org/schema/websocket
			https://www.springframework.org/schema/websocket/spring-websocket.xsd">

	<websocket:message-broker application-destination-prefix="/app">
		<websocket:stomp-endpoint path="/portfolio" />
		<websocket:simple-broker prefix="/topic, /queue"/>
	</websocket:message-broker>

</beans>
对于内置的简单代理(simple broker),/topic/queue 前缀没有任何特殊含义。它们只是一种约定,用于区分发布-订阅(pub-sub)模式与点对点(point-to-point)消息传递(即多个订阅者与单一消费者)。当你使用外部代理时,请查阅该代理的 STOMP 页面,以了解它支持哪些类型的 STOMP 目标地址和前缀。

要从浏览器连接以使用 STOMP,您可以使用 stomp-js/stompjs,这是目前维护最积极的 JavaScript 库。spring-doc.cadn.net.cn

以下示例代码基于它编写:spring-doc.cadn.net.cn

const stompClient = new StompJs.Client({
	brokerURL: 'ws://domain.com/portfolio',
	onConnect: () => {
		// ...
	}
});

或者,如果你通过 SockJS 进行连接,可以在服务器端通过 xref page 启用 SockJS 回退机制,并在 JavaScript 端按照 这些说明进行操作。spring-doc.cadn.net.cn

请注意,上例中的 stompClient 无需指定 loginpasscode 头部。即使指定了,这些头部也会在服务器端被忽略(更准确地说,会被覆盖)。有关认证的更多信息,请参阅连接到代理认证spring-doc.cadn.net.cn

更多示例代码请参见:spring-doc.cadn.net.cn