1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| @Component @ServerEndpoint(value = "/websocket/{ip}") public class MyWebSocket { private static final Logger log = LoggerFactory.getLogger(MyWebSocket.class); private static int onlineCount = 0; private static ConcurrentHashMap<String, MyWebSocket> webSocketMap = new ConcurrentHashMap<String, MyWebSocket>(); private Session session; private String ip; public static final String ACTION_PRINT_ORDER = "printOrder"; public static final String ACTION_SHOW_PRINT_EXPORT = "showPrintExport";
@OnOpen public void onOpen(Session session, @PathParam("ip") String ip) { this.session = session; this.ip = ip; webSocketMap.put(ip, this); addOnlineCount();
log.info("有新连接加入,ip:{}!当前在线人数为:{}", ip, getOnlineCount()); ExportService es = BeanUtils.getBean(ExportService.class); List<String> list = es.listExportCodesByPrintIp(ip); ResponseData<String> rd = new ResponseData<String>(); rd.setAction(MyWebSocket.ACTION_SHOW_PRINT_EXPORT); rd.setList(list); sendObject(rd); }
@OnClose public void onClose(@PathParam("ip") String ip) { webSocketMap.remove(ip); subOnlineCount(); log.info("websocket关闭,IP:{},当前在线人数为:{}", ip, getOnlineCount()); }
@OnMessage public void onMessage(String message, Session session) {
log.debug("websocket来自客户端的消息:{}", message); OrderService os = BeanUtils.getBean(OrderService.class); OrderVo ov = os.getOrderDetailByOrderNo(message);
ResponseData<OrderVo> rd = new ResponseData<OrderVo>(); ArrayList<OrderVo> list = new ArrayList<OrderVo>(); list.add(ov); rd.setAction(MyWebSocket.ACTION_PRINT_ORDER); rd.setList(list); sendObject(rd);
}
@OnError public void onError(Session session, Throwable error) {
log.error("webSocket发生错误!IP:{}", ip); error.printStackTrace(); }
public void sendMessage(String message) { try { this.session.getBasicRemote().sendText(message); } catch (IOException e) { e.printStackTrace(); log.error("发送数据错误,ip:{},msg:{}", ip, message); } }
public void sendObject(Object obj) { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(Include.NON_NULL); String s = null; try { s = mapper.writeValueAsString(obj); } catch (JsonProcessingException e) { e.printStackTrace(); log.error("转json错误!{}", obj); } this.sendMessage(s); }
public static void sendInfo(String message) { for (Entry<String, MyWebSocket> entry : webSocketMap.entrySet()) { MyWebSocket value = entry.getValue(); value.sendMessage(message); } } public static synchronized int getOnlineCount() { return onlineCount; } public static synchronized void addOnlineCount() { MyWebSocket.onlineCount++; } public static synchronized void subOnlineCount() { MyWebSocket.onlineCount--; } public static ConcurrentHashMap<String, MyWebSocket> getWebSocketMap() { return webSocketMap; } }
|