WebSocketStompClient won't connect to SockJS endpoint

后端 未结 3 1152
心在旅途
心在旅途 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 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();
    }
    

提交回复
热议问题