/** * deprecated: 接收到客户端连接请求时调用 * * <p>date 2015-03-06 * * @author sharkTang * @param session */ @OnOpen public void onOpen(Session session) { try { // 每次open一个session时,赋值到全局WebsocketSession,保持统一的全局session数据 daPingWebsocketSession = session; webSocketMapAction(webSessionMap, session, WebSocketConstants.ADD_STR); // 往websocketManger添加一份数据 WebSocketManager.webSocketClientCount++; WebSocketManager.webQueryStringSet.add(session.getQueryString()); /* * 每创建一个连接,将session信息保存到全局HashMap Key: session的hashcode, * value:组装的sessionBo 并对全局Map数据加锁 */ WebSocketBo webSocketBo = new WebSocketBo(); webSocketBo.setWsId(UUIDUtils.getUUID()); webSocketBo.setMessageStatus(WebSocketConstants.MESSAGESTATUSNO); // 初始化时设为消息状态为no状态 int webSocketHashCode = session.hashCode(); webSocketBo.setWsHashCode(webSocketHashCode); String pageType = session.getQueryString(); if (null == pageType) { pageType = WebSocketConstants.SPACESTR_ENG; } webSocketBo.setPageType(pageType); webSocketAction(webSocketMap, session, WebSocketConstants.ADD_STR, webSocketBo); // 发送初始化数据 // String sendDataString = DataInitServer.getNewInitData(); // session.getAsyncRemote().sendText(sendDataString); } catch (Exception e) { } }
/** * @OnOpen allows us to intercept the creation of a new session. The session class allows us to * send data to the user. In the method onOpen, we'll let the user know that the handshake was * successful. */ @OnOpen public void onOpen(Session session) { Logger logger = LogManager.getFormatterLogger(EchoService.class); logger.info(session.getId() + " has opened a connection"); logger.info("MaxBinaryMessageBufferSize %d", session.getMaxBinaryMessageBufferSize()); logger.info("MaxIdleTimeout %d", session.getMaxIdleTimeout()); logger.info("MaxTextMessageBufferSize %d", session.getMaxTextMessageBufferSize()); logger.info("Open sessions size %d", session.getOpenSessions().size()); logger.info("Protocol %s", session.getProtocolVersion()); logger.info("QueryString %s", session.getQueryString()); try { session.getBasicRemote().sendText("Connection Established"); } catch (IOException ex) { ex.printStackTrace(); logger.error(ex.getMessage(), ex); } }
/** * deprecated: 接收到客户端关闭时调用 * * <p>date 2015-03-06 * * @author sharkTang * @param session * @param closeReason */ @OnClose public void onClose(Session session, CloseReason closeReason) { try { logger_.info( "Web-socket session " + session.getId() + " sessionHashCode" + session.hashCode() + " closed, reason: " + closeReason.toString()); WebSocketManager.webSocketClientCount--; WebSocketManager.webQueryStringSet.remove(session.getQueryString()); // 关闭客户端 赋值全局session,并删除websocket对象 webSocketAction(webSocketMap, session, WebSocketConstants.REMOVE_STR, null); daPingWebsocketSession = session; webSocketMapAction(webSessionMap, session, WebSocketConstants.REMOVE_STR); } catch (Exception ex) { } }