1 importjava.io.IOException;2 importjava.util.Map;3 importjava.util.concurrent.CopyOnWriteArraySet;4
5 importjavax.annotation.PostConstruct;6 importjavax.websocket.OnClose;7 importjavax.websocket.OnError;8 importjavax.websocket.OnMessage;9 importjavax.websocket.OnOpen;10 importjavax.websocket.Session;11 importjavax.websocket.server.PathParam;12 importjavax.websocket.server.ServerEndpoint;13
14 importcn.th.jump.demoboot.service.ClockBookingService;15 importcn.th.jump.demoboot.service.OsInspectionServiceImpl;16 importcom.alibaba.fastjson.JSONObject;17 importorg.springframework.beans.factory.annotation.Autowired;18 importorg.springframework.stereotype.Component;19 importcn.hutool.log.Log;20 importcn.hutool.log.LogFactory;21
22
23 @ServerEndpoint("/websocket")24 @Component25 public classWebSocketServer {26
27 // https://blog.csdn.net/moshowgame/article/details/80275084
28
29 static Log log=LogFactory.get(WebSocketServer.class);30 //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
31 private static int onlineCount = 0;32 //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
33 private static CopyOnWriteArraySet webSocketSet = new CopyOnWriteArraySet();34
35 //与某个客户端的连接会话,需要通过它来给客户端发送数据
36 privateSession session;37
38 public staticWebSocketServer webSocketServer;39
40 @PostConstruct41 public voidinit() {42 webSocketServer = this;43 }44
45 @Autowired46 OsInspectionServiceImpl osInspectionService;47
48 //接收sid
49 private String sid="";50 /**
51 * 连接建立成功调用的方法*/
52 @OnOpen53 public void onOpen(Session session,@PathParam("sid") String sid) {54 this.session =session;55 webSocketSet.add(this); //加入set中
56 addOnlineCount(); //在线数加157 //log.info("有新窗口开始监听:"+sid+",当前在线人数为" + getOnlineCount());
58 this.sid=sid;59 try{60 sendMessage("连接成功");61 } catch(IOException e) {62 log.error("websocket IO异常");63 }64 }65
66 /**
67 * 连接关闭调用的方法68 */
69 @OnClose70 public voidonClose() {71 webSocketSet.remove(this); //从set中删除
72 subOnlineCount(); //在线数减1
73 log.info("有一连接关闭!当前在线人数为" +getOnlineCount());74 }75
76 /**
77 * 收到客户端消息后调用的方法78 *79 *@parammessage 客户端发送过来的消息*/
80 @OnMessage81 public voidonMessage(String message, Session session) {82 log.info("收到来自窗口"+sid+"的信息:"+message);83 //群发消息
84 for(WebSocketServer item : webSocketSet) {85 try{86
87 /*巡检设备ping通状态 已修改为直接跑脚本形式88 JSONObject jsonObject = JSONObject.parseObject(message);89 Map map = jsonObject;90 log.info( jsonObject.toString() );91 log.info( map.get("unconnected").toString() );92
93 webSocketServer.osInspectionService.batchPing(map);*/
94
95 item.sendMessage("fresh-");96
97 } catch(IOException e) {98 e.printStackTrace();99 }100 }101 }102
103 /**
104 *105 *@paramsession106 *@paramerror107 */
108 @OnError109 public voidonError(Session session, Throwable error) {110 log.error("发生错误");111 error.printStackTrace();112 }113 /**
114 * 实现服务器主动推送115 */
116 public void sendMessage(String message) throwsIOException {117 //this.session.getBasicRemote().sendText(message);
118 this.session.getBasicRemote().sendText(message);119 }120
121
122 /**
123 * 群发自定义消息124 **/
125 public static void sendInfo(String message) throwsIOException {126 log.info("推送消息到窗口 "+message,message);127 for(WebSocketServer item : webSocketSet) {128 try{129 //这里可以设定只推送给这个sid的,为null则全部推送
130 item.sendMessage(message);131 } catch(IOException e) {132 continue;133 }134 }135 }136
137 public static synchronized intgetOnlineCount() {138 returnonlineCount;139 }140
141 public static synchronized voidaddOnlineCount() {142 WebSocketServer.onlineCount++;143 }144
145 public static synchronized voidsubOnlineCount() {146 WebSocketServer.onlineCount--;147 }148 }