WebSocketStompClient won't connect to SockJS endpoint

前提是你 提交于 2019-12-03 02:24:10

The following configuration works:

RequestUpgradeStrategy upgradeStrategy = new TomcatRequestUpgradeStrategy();
registry.addEndpoint("/hello")
        .withSockJS();

registry.addEndpoint("/hello")
        .setHandshakeHandler(new DefaultHandshakeHandler(upgradeStrategy))
        .setAllowedOrigins("*");

Not sure if this is by design (i.e. two endpoints with the same path)?

A better solution based on Brian-Clozel's feedback is to use the java SockJsClient as a transport when configuring the WebSocketStompClient:

List<Transport> transports = new ArrayList<>(1);
transports.add(new WebSocketTransport( new StandardWebSocketClient()) );
WebSocketClient transport = new SockJsClient(transports);
WebSocketStompClient stompClient = new WebSocketStompClient(transport);

which allows the use of only one endpoint to which the java and javascript clients can both connect:

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

As stated in the reference documentation: when enabling SockJS, multiple transports are configured for you. First, the client sends a "GET /info" request to know about supported transports and then sends a request depending on its capabilities:

"http://host:port/myApp/myEndpoint/{server-id}/{session-id}/{transport}"

Now you don't need to duplicate your configuration for you "/hello" endpoint. You'd rather directly use the websocket endpoint, or better, use the Java SockJS client with a websocket transport. See the complete example in the reference documentation.

You can check the http filter(such as shiro filter) or HandlerInterceptor.

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