启用STOMP

基于WebSocket的STOMP协议支持已在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>
对于内置的简单代理,/topic/queue 前缀没有任何特殊含义。它们仅仅是一种约定,用于区分发布订阅与点对点消息传递(即多个订阅者与一个消费者)。当你使用外部代理时,请检查代理的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连接,可以在服务器端启用 SockJS回退功能(设置 registry.addEndpoint("/portfolio").withSockJS()),并在JavaScript端按照 这些说明进行操作。spring-doc.cadn.net.cn

请注意,在前面的例子中,stompClient 不需要指定 loginpasscode 标头。即使指定了,它们也会在服务器端被忽略(或者更确切地说,被覆盖)。有关身份验证的更多信息,请参见 连接到代理身份验证spring-doc.cadn.net.cn

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