Websockets:是否可以使用SockJS添加多个端点?

时间:2014-10-06 06:59:38

标签: websocket sockjs spring-websocket

我想创建2个Web套接字端点。你能告诉它有可能吗?

那种情况下的配置是什么?

1 个答案:

答案 0 :(得分:3)

如果你使用普通的websockets或STOMP消息,你的问题并不清楚。

普通websocket API

如果您使用的是plain websocket API,则注册表API允许您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):

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/foo", "/bar").withSockJS();
}