1
0
mirror of https://github.com/Snailclimb/JavaGuide synced 2025-06-16 18:10:13 +08:00

Updating the process of configuring WebSocket in Spring Boot.

This commit is contained in:
smy1999 2024-03-09 15:42:38 +08:00
parent 14bd566b5c
commit 0784c33a68

View File

@ -374,6 +374,22 @@ public class WebSocketServer {
}
```
服务端还需要注入`ServerEndpointerExporter`,这个 Bean 就会自动注册使用了`@ServerEndpoint`注解的 WebSocket 服务器。
```java
@Configuration
public class WebSocketConfiguration {
/**
* 用于注册使用了 @ServerEndpoint 注解的 WebSocket 服务器
*/
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
```
前端初始化打开 WebSocket 连接,并监听连接状态,接收服务端数据或向服务端发送数据。
```javascript