启用 STOMP
STOMP over WebSocket 支持在 spring-messaging
和 spring-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>
备注
对于内置的简单代理,/topic
和 /queue
前缀没有任何特殊含义。它们仅仅是一种约定,用于区分发布-订阅与点对点消息传递(即,多个订阅者与一个消费者)。当你使用外部代理时,请查看该代理的 STOMP 页面,以了解它支持什么类型的 STOMP 目标和前缀。
要从浏览器连接,对于 STOMP,您可以使用 stomp-js/stompjs,这是一个最活跃维护的 JavaScript 库。
以下示例代码基于此:
const stompClient = new StompJs.Client({
brokerURL: 'ws://domain.com/portfolio',
onConnect: () => {
// ...
}
});
或者,如果您通过 SockJS 连接,您可以在服务器端启用 SockJS Fallback,使用 registry.addEndpoint("/portfolio").withSockJS()
,在 JavaScript 端,按照 这些说明 操作。
注意,在前面的示例中,stompClient
不需要指定 login
和 passcode
头。即使指定了,它们也会在服务器端被忽略(或者说,被覆盖)。有关身份验证的更多信息,请参见 Connecting to a Broker 和 Authentication。
有关更多示例代码,请参见:
-
使用 WebSocket 构建交互式 web 应用程序 — 入门指南。
-
股票投资组合 — 示例应用程序。