启用 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 连接,您可以在服务器端使用 registry.addEndpoint("/portfolio").withSockJS() 启用 SockJS 回退,并在 JavaScript 端按照 这些说明 进行操作。
请注意,前面示例中的 stompClient 无需指定 login 和 passcode 标头。即使指定了,它们也会在服务器端被忽略(或者说被覆盖)。有关身份验证的更多信息,请参阅 连接到代理 和 身份验证。
有关更多示例代码,请参阅
-
使用 WebSocket 构建交互式 Web 应用程序 — 入门指南。
-
股票投资组合 — 一个示例应用程序。