啟用 STOMP
在 spring-messaging
和 spring-websocket
模組中提供透過 WebSocket 的 STOMP 支援。一旦您擁有這些相依性,您就可以透過 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>
對於內建的簡單 Broker,/topic 和 /queue 前綴沒有任何特殊意義。它們只是一種區分發布-訂閱與點對點訊息傳遞的慣例(也就是說,多個訂閱者與一個消費者)。當您使用外部 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 建置互動式網路應用程式 — 入門指南。
-
股票投資組合 — 範例應用程式。