天天看点

socket python 连接 java_WebSocket的java和python使用

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

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

69 @OnClose70 public voidonClose() {71 webSocketSet.remove(this); //从set中删除

72 subOnlineCount(); //在线数减1

73 log.info("有一连接关闭!当前在线人数为" +getOnlineCount());74 }75

76

80 @OnMessage81 public voidonMessage(String message, Session session) {82 log.info("收到来自窗口"+sid+"的信息:"+message);83 //群发消息

84 for(WebSocketServer item : webSocketSet) {85 try{86

87

94

95 item.sendMessage("fresh-");96

97 } catch(IOException e) {98 e.printStackTrace();99 }100 }101 }102

103

108 @OnError109 public voidonError(Session session, Throwable error) {110 log.error("发生错误");111 error.printStackTrace();112 }113

116 public void sendMessage(String message) throwsIOException {117 //this.session.getBasicRemote().sendText(message);

118 this.session.getBasicRemote().sendText(message);119 }120

121

122

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 }