【问题标题】:Java WebSocket Client Spring bootJava WebSocket 客户端 Spring 启动
【发布时间】:2018-09-10 10:47:38
【问题描述】:

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

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

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

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

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

这是我在 Spring Boot 客户端 App 中创建的 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

知道我做错了什么吗?

【问题讨论】:

  • 你有这个示例 WebSocket 客户端项目吗?

标签: java spring-boot stomp spring-websocket


【解决方案1】:

我终于想通了。它失败的原因是你需要把它放在 URL 中

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

点赞URI.create("ws://localhost:8080/gs-guide-websocket")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 2015-10-26
    相关资源
    最近更新 更多