Websockets:是否可以使用 SockJS 添加多个端点? [英] Websockets: Is it possible to add multiple Endpoints using SockJS?
问题描述
我想创建 2 个网络套接字端点.你能说有可能吗?
I want to create 2 web socket endpoints. Can you tell is it possible?
在那种情况下应该是什么配置?
What shall be the configuration in that case?
推荐答案
您的问题没有明确说明您使用的是普通 websocket 还是 STOMP 消息传递.
Your question does not clearly states if you're using plain websockets or STOMP messaging.
如果您使用普通的 websocket API,注册 API 允许您添加任意数量的 websocket 处理程序.
If you're using the plain websocket API, the registry API allows you to add as many websocket handlers as you want.
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(myFirstHandler(), "/myHandler1").withSockJS();
registry.addHandler(mySecondHandler(), "/myHandler2").withSockJS();
}
@Bean
public WebSocketHandler myFirstHandler() {
return new MyFirstHandler();
}
@Bean
public WebSocketHandler mySecondHandler() {
return new MySecondHandler();
}
}
STOMP 端点
如果您正在使用 STOMP 并且想要添加多个 STOMP 端点,那么 API 也允许您这样做(addEndpoint
方法接受一个 String vararg):
STOMP endpoints
If you're using STOMP and would like to add several STOMP endpoints, then the API also allows you to do that (the addEndpoint
method accepts a String vararg):
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/foo", "/bar").withSockJS();
}
这篇关于Websockets:是否可以使用 SockJS 添加多个端点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!