简体   繁体   中英

Java WebSocket Client Spring boot

I want to create a spring boot app with a web socket client that connects to a web socket server.

As an example, I used the Getting Started guide you can find in Spring Boot.

https://spring.io/guides/gs/messaging-stomp-websocket/

In this example, you create a web socket server using spring boot and you connect to it using JavaScript.

I want to run that server and connect to it using another spring boot application that creates a WebSocketClient object.

This is the WebSocketClientConfiguration class that I created in the Spring Boot client App

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketClientConfig {

    @Bean
    public WebSocketClient webSocketClient() {
        final WebSocketClient client = new StandardWebSocketClient();

        final WebSocketStompClient stompClient = new WebSocketStompClient(client);
        stompClient.setMessageConverter(new MappingJackson2MessageConverter());

        final StompSessionHandler sessionHandler = new MyStompSessionHandler();
        stompClient.connect("ws://localhost:8080", sessionHandler);
        return client;
    }
}

But in my class MyStompSessionHandler, in handleTransportError method I can see that the exception is

javax.websocket.DeploymentException: The HTTP response from the server [200] did not permit the HTTP upgrade to WebSocket

Any idea what I am doing wrong?

I finally figured out. The reason it was failing it is because you need to put this in the URL

ws://localhost:8080/gs-guide-websocket

Like this URI.create("ws://localhost:8080/gs-guide-websocket")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM