提问者:小点点

JavaWebSocket客户端Spring启动


我想创建一个带有连接到Web套接字服务器的Web套接字客户端的Spring引导应用程序。

例如,我使用了您可以在Spring Boot中找到的入门指南。

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

在此示例中,您使用Spring boot创建一个Web套接字服务器,并使用JavaScript连接到它。

我想运行该服务器并使用另一个创建WebSocketClient对象的Spring引导应用程序连接到它。

这是我在Spring Boot客户端应用程序中创建的WebSocketClientConfiguration类

@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;
    }
}

但是在我的类MyStompSessionHandler中,在handleTransportError方法中,我可以看到异常是

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

知道我做错了什么吗?


共1个答案

匿名用户

我终于明白了。它失败的原因是因为你需要把这个放在URL

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

像这样URI. create("ws://localhost:8080/gs-guide-websocket")