WebSocketStompClient won't connect to SockJS endpoint

后端 未结 3 1153
心在旅途
心在旅途 2021-02-04 10:10

I\'m playing around with the new (as of version 4.2) java STOMP client support. My starting point is the getting started guide (Using WebSocket to build an interactive web appl

相关标签:
3条回答
  • 2021-02-04 10:46

    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();
    }
    
    0 讨论(0)
  • 2021-02-04 10:49

    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.

    0 讨论(0)
  • 2021-02-04 10:50

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

    0 讨论(0)
提交回复
热议问题