首先添加一下 pom 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
然后添加一个 WebSocketConfig 配置类
/**
* @author wuwx
*/
@EnableWebSocket
@Configuration
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new TextWebSocketHandler(), "/websocket");
}
}
启动这个 SpringBoot 项目就可以在 /websocket 进行 WebSocket 连接了