|  | @ -1,12 +1,11 @@
 | 
												
													
														
															|  | package com.yihu.jw.care.endpoint.third.platForm;
 |  | package com.yihu.jw.care.endpoint.third.platForm;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | import com.yihu.jw.care.service.third.PcManageWebSocketService;
 | 
												
													
														
															|  | import org.springframework.web.bind.annotation.RestController;
 |  | import org.springframework.web.bind.annotation.RestController;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | import javax.websocket.*;
 |  | import javax.websocket.*;
 | 
												
													
														
															|  | import javax.websocket.server.ServerEndpoint;
 |  | import javax.websocket.server.ServerEndpoint;
 | 
												
													
														
															|  | import java.io.IOException;
 |  | import java.io.IOException;
 | 
												
													
														
															|  | import java.util.ArrayList;
 |  | 
 | 
												
													
														
															|  | import java.util.Collection;
 |  | 
 | 
												
													
														
															|  | import java.util.Collections;
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | /**
 |  | /**
 | 
												
													
														
															|  |  * Created by Bing on 2021/6/17.
 |  |  * Created by Bing on 2021/6/17.
 | 
												
											
												
													
														
															|  | @ -18,14 +17,14 @@ import java.util.Collections;
 | 
												
													
														
															|  | @ServerEndpoint("/cloudCare/noLogin/pcManage/websocket")
 |  | @ServerEndpoint("/cloudCare/noLogin/pcManage/websocket")
 | 
												
													
														
															|  | public class PcManageWebSocketServer {
 |  | public class PcManageWebSocketServer {
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     private static Collection<PcManageWebSocketServer> servers = Collections.synchronizedCollection(new ArrayList<PcManageWebSocketServer>());
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     //与某个客户端的连接会话,需要通过它来给客户端发送数据
 |  |     //与某个客户端的连接会话,需要通过它来给客户端发送数据
 | 
												
													
														
															|  |     private Session session;
 |  |     private Session session;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     @OnOpen
 |  |     @OnOpen
 | 
												
													
														
															|  |     public void onOpen(Session session){
 |  |     public void onOpen(Session session){
 | 
												
													
														
															|  |         this.session = session;
 |  |         this.session = session;
 | 
												
													
														
															|  |         add(this);
 |  | 
 | 
												
													
														
															|  | 
 |  |         PcManageWebSocketService.add(this);
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     public void sendMessage(String message) throws IOException {
 |  |     public void sendMessage(String message) throws IOException {
 | 
												
											
												
													
														
															|  | @ -34,7 +33,7 @@ public class PcManageWebSocketServer {
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     @OnClose
 |  |     @OnClose
 | 
												
													
														
															|  |     public void onClose(){
 |  |     public void onClose(){
 | 
												
													
														
															|  |         remove(this);
 |  | 
 | 
												
													
														
															|  | 
 |  |         PcManageWebSocketService.remove(this);
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     @OnMessage
 |  |     @OnMessage
 | 
												
											
												
													
														
															|  | @ -48,26 +47,5 @@ public class PcManageWebSocketServer {
 | 
												
													
														
															|  |         error.printStackTrace();
 |  |         error.printStackTrace();
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     public static void broadCast(String msg){
 |  | 
 | 
												
													
														
															|  |         for (PcManageWebSocketServer bitCoinServer : servers) {
 |  | 
 | 
												
													
														
															|  |             try {
 |  | 
 | 
												
													
														
															|  |                 bitCoinServer.sendMessage(msg);
 |  | 
 | 
												
													
														
															|  |             } catch (IOException e) {
 |  | 
 | 
												
													
														
															|  |                 e.printStackTrace();
 |  | 
 | 
												
													
														
															|  |             }
 |  | 
 | 
												
													
														
															|  |         }
 |  | 
 | 
												
													
														
															|  |     }
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     public static int getTotal(){
 |  | 
 | 
												
													
														
															|  |         return servers.size();
 |  | 
 | 
												
													
														
															|  |     }
 |  | 
 | 
												
													
														
															|  |     public static void add(PcManageWebSocketServer server){
 |  | 
 | 
												
													
														
															|  |         System.out.println("有新连接加入! 当前总连接数是:"+ servers.size());
 |  | 
 | 
												
													
														
															|  |         servers.add(server);
 |  | 
 | 
												
													
														
															|  |     }
 |  | 
 | 
												
													
														
															|  |     public static void remove(PcManageWebSocketServer server){
 |  | 
 | 
												
													
														
															|  |         System.out.println("有连接退出! 当前总连接数是:"+ servers.size());
 |  | 
 | 
												
													
														
															|  |         servers.remove(server);
 |  | 
 | 
												
													
														
															|  |     }
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | }
 |  | }
 |