1.添加websocket依赖到pom文件
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency>
2.开启websocket支持
@Configuration public class WebSocketConfig { @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } }
3.创建工具类
@ServerEndpoint("/websocket/{id}") @Component public class WebSocketUtils { //记录在线连接数 private static int onlineCount = 0; //区分多个连接 private static ConcurrentHashMap<Integer,WebSocketUtils> wsMap = new ConcurrentHashMap<>(); //唯一会话 private Session session; /** * 获取map */ public static ConcurrentHashMap<Integer,WebSocketUtils> getWebsocketMap(){ return wsMap; } /** * 连接成功 *