此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0! |
启用 STOMP
STOMP over WebSocket 支持在spring-messaging
和spring-websocket
模块。拥有这些依赖项后,您可以公开 STOMP
endpoint,如下例所示:
-
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 的约定
消息收发(即,多个订阅者与 1 个使用者)。当您使用外部代理时,
检查代理的 STOMP 页面以了解哪种类型的 STOMP 目标和
它支持的前缀。 |
要从浏览器进行连接,对于 STOMP,您可以使用stomp-js/stompjs
哪个是最
积极维护的 JavaScript 库。
以下示例代码基于它:
const stompClient = new StompJs.Client({
brokerURL: 'ws://domain.com/portfolio',
onConnect: () => {
// ...
}
});
或者,如果你通过 Sockjs 连接,你可以在服务器端启用 Sockjs 回退registry.addEndpoint("/portfolio").withSockJS()
在 JavaScript 方面,
按照这些说明进行作。
请注意,stompClient
在前面的示例中不需要指定login
和passcode
头。即使这样做了,他们也会被忽略(或者更确切地说,
overridden) 的有关身份验证的更多信息,请参见Connecting to a Broker and Authentication。
有关更多示例代码,请参阅:
-
使用 WebSocket 构建 交互式 Web 应用程序 — 入门指南。
-
Stock Portfolio — 示例 应用。