【发布时间】: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