Websockets: Is it possible to add multiple Endpoints using SockJS?

▼魔方 西西 提交于 2020-01-16 18:48:51

问题


I want to create 2 web socket endpoints. Can you tell is it possible?

What shall be the configuration in that case?


回答1:


Your question does not clearly states if you're using plain websockets or STOMP messaging.

Plain websocket API

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 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();
}


来源:https://stackoverflow.com/questions/26211248/websockets-is-it-possible-to-add-multiple-endpoints-using-sockjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!