Enable STOMP
WebSocket支持的STOMP在`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>
对于内置简单代理, |
要从浏览器进行连接,对于STOMP,你可以使用 link:https://github.com/stomp-js/stompjs[stomp-js/stompjs
,这是维护最活跃的JavaScript库。
以下示例代码基于它:
const stompClient = new StompJs.Client({
brokerURL: 'ws://domain.com/portfolio',
onConnect: () => {
// ...
}
});
或者,如果你通过 SockJS 连接,可以在服务端通过 `registry.addEndpoint("/portfolio").withSockJS()`启用 SockJS Fallback,并通过执行 those instructions在 JavaScript 端启用。
请注意,在上述示例中,`stompClient`无需指定 `login`和 `passcode`标题。如果确实指定,它们将在服务端被忽略(或更准确地说,被覆盖)。请参阅 Connecting to a Broker和 Authentication以了解有关身份验证的更多信息。
有关更多示例代码,请参阅:
-
使用 WebSocket 构建交互式 Web 应用程序——一个入门指南。
-
Stock Portfolio — a sample application.