啟用 STOMP
spring-messaging
和 spring-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
不需要指定 login
和 passcode
頭部。即使指定了,它們在服務端也會被忽略(或者說被覆蓋)。有關認證的更多資訊,請參閱 連線到 Broker 和 認證。
更多示例程式碼請參閱
-
使用 WebSocket 構建互動式 Web 應用程式 — 入門指南。
-
股票投資組合 — 一個示例應用程式。