天天看點

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 }