啟用 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 應用程式 — 入門指南。
-
股票投資組合 — 一個示例應用程式。