【问题标题】:websocket spring boot setupwebsocket弹簧启动设置
【发布时间】:2022-01-04 18:19:35
【问题描述】:

我有一个 Spring Boot 应用程序。我正在尝试将 websocket 片段添加到其中。问题是我的角度客户端无法连接到它。我使用了智能 websocket 客户端谷歌插件,但仍然无法连接。这是设置。

我在本地主机上使用 Intellij Idea。 Spring Boot 应用程序在 localhost:8080 上运行。我可以从 intellij idea 控制台看到 WebSocketSession 正在运行。

设置如下:


    @Slf4j
    @RestController
    public class WebsocketController {

         @MessageMapping("/ws-on/hello")
         @SendTo("/ws-on/greetings")
    public UserStateIndicator greeting(UserStateIndicator indicator) throws Exception {
        Thread.sleep(1000); // simulated delay
        log.debug("websocket " + indicator.toString());
        return indicator;
    }
}




@Configuration
@EnableWebSocketMessageBroker
public class WebsocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/ws-on");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws-on")
                .setAllowedOrigins("http://localhost:4200")
                .withSockJS();
    }
}

我的 Angular 在 localhost:4200 上运行。 我使用 ws://localhost:8080/ws-on 作为 StompJS 的 url 进行连接。 我的问题是如何找到要连接的websocket url,如何知道websocket 正在spring boot 服务器上运行?

【问题讨论】:

    标签: spring-boot websocket


    【解决方案1】:

    我终于明白了。因为我在 Angular 和 Spring Boot 上都使用 SockJS,所以 URL 实际上是 http 而不是 ws。正确的连接网址是 http://localost:8080/ws-on

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-08
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      • 2018-09-27
      • 1970-01-01
      • 2016-03-23
      • 1970-01-01
      相关资源
      最近更新 更多