此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10spring-doc.cadn.net.cn

启用 STOMP

STOMP over WebSocket 支持在spring-messagingspring-websocket模块。一旦你有了这些依赖关系,你就可以公开一个 STOMP endpoint over WebSocket,如以下示例所示: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 Fallbackregistry.addEndpoint("/portfolio").withSockJS()在 JavaScript 方面, 通过遵循这些说明spring-doc.cadn.net.cn

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

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