启用 STOMP

spring-messagingspring-websocket 模块提供了 STOMP over WebSocket 的支持。一旦你拥有了这些依赖项,你就可以通过 WebSocket 暴露一个 STOMP 端点,示例如下

  • Java

  • Kotlin

  • Xml

@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 (点对点) 消息传递 (即多订阅者对比单消费者) 的约定。当你使用外部 broker 时,请查阅该 broker 的 STOMP 文档,了解它支持的 STOMP 目的地和前缀类型。

要从浏览器连接,对于 STOMP,你可以使用 stomp-js/stompjs,它是最活跃维护的 JavaScript 库。

以下示例代码基于它

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

或者,如果你通过 SockJS 连接,你可以在服务端使用 registry.addEndpoint("/portfolio").withSockJS() 启用 SockJS 回退,并在 JavaScript 端按照 这些说明 进行操作。

请注意,前一个示例中的 stompClient 不需要指定 loginpasscode 头部。即使指定了,它们在服务端也会被忽略(或者说被覆盖)。有关认证的更多信息,请参阅 连接到 Broker认证

更多示例代码请参阅