1 <dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-websocket</artifactId>
4 </dependency>
1 @Configuration
2 public class WebSocketConfig {
3 !
4 @Bean
5 public ServerEndpointExporter serverEndpointExporter() {
6 return new ServerEndpointExporter();
7 }
8 !
9 }
1 @Slf4j
2 @ServerEndpoint("/websocket/{sid}")
3 @Component
4 public class WebSocketServer {
5 !
6 !
7 //静态变,来记录当前在线连接数。应该把它设计成线程安全的。
8 private static int onlineCount = 0;
9 //concurrent包的线程安全Set,来存放每个客户端对应的MyWebSocket对象。
10 private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<We
bSocketServer>();
11 !
12 //与某个客户端的连接会话,需要通过它来给客户端发送数据
13 private Session session;
14 !
15 //接收sid
评论0