@Override protected void onOpen(WsOutbound outbound) { if(uid == -1){ //过期用户直接下线 Map<String, Object> outMap = new HashMap<String, Object>(); outMap.put("response", "notlogin"); String jsonString = JSONObject.fromObject(outMap).toString(); CharBuffer buffer = CharBuffer.wrap(jsonString); try { outbound.writeTextMessage(buffer); outbound.flush(); } catch (IOException e) { e.printStackTrace(); } }else{ userMap.put(uid, outbound); System.out.println("[上线]==>uid:"+uid+"在线用户==>"+userMap.size()); sendUnread(outbound); //登录即检查有没有未读消息 } super.onOpen(outbound); }
private void sendUnread(WsOutbound outbound){ Map<String, Object> outMap = new HashMap<String, Object>(); outMap.put("response", "remind_unread"); UnreadDAO dao = new UnreadDAOimpl(); List<UnreadVo> list = dao.getUnread(uid); if(list != null){ outMap.put("remind_unread", list); String jsonString = JSONObject.fromObject(outMap).toString(); CharBuffer buffer = CharBuffer.wrap(jsonString); try { outbound.writeTextMessage(buffer); outbound.flush(); } catch (IOException e) { e.printStackTrace(); } } }
@SuppressWarnings("unchecked") @Override public void attributeAdded(ServletContextAttributeEvent arg0) { if(userMap == null){ userMap = (Map<Integer, WsOutbound>) arg0.getServletContext().getAttribute("OnLineList"); } //System.out.println("listener==>attributeAdded"); Enumeration<String> att = arg0.getServletContext().getAttributeNames(); while(att.hasMoreElements()){ String next = att.nextElement(); if(next.startsWith("action")){ ServerMsg message = (ServerMsg) arg0.getServletContext().getAttribute(next); if(message != null){ arg0.getServletContext().removeAttribute(next); doAction(message); } } } }
private void sendUnread(UnreadVo unread, WsOutbound outbound) { Map<String, Object> outMap = new HashMap<String, Object>(); outMap.put("response", "remind_unread"); List<UnreadVo> list = new ArrayList<UnreadVo>(); list.add(unread); outMap.put("remind_unread", list); String jsonString = JSONObject.fromObject(outMap).toString(); CharBuffer buffer = CharBuffer.wrap(jsonString); try { outbound.writeTextMessage(buffer); outbound.flush(); } catch (IOException e) { e.printStackTrace(); } }
private void action10(ServerMsg message){ EventFollowerVo action = (EventFollowerVo) message.getAction(); int eid = action.getEid(); String uname = action.getUname(); dao.countFollower(eid); //活动参与人数计数+1 //发送给活动创建者 int euid = dao.getEventUID(eid); WsOutbound outbound = userMap.get(euid); if(outbound != null){ UnreadVo unread = dao.putUnread(euid,3,action.getEid(),0,uname+"加入了你的活动",DateTimeUtil.currentTime(),true); unread.setIsread(false); sendUnread(unread,outbound); }else{ dao.putUnread(euid,3,action.getEid(),0,uname+"加入了你的活动",DateTimeUtil.currentTime(),false); } }
public void action15(ServerMsg message){ EventVo action = (EventVo) message.getAction(); int eid = action.getEid(); List<Integer> uids = dao.getAllFollowerUser(eid); //通知所有参与的用户 for(int uid:uids){ WsOutbound outbound = userMap.get(uid); if(outbound != null){ UnreadVo unread = dao.putUnread(uid,4,eid,1,action.getOutline(),DateTimeUtil.currentTime(),true); unread.setIsread(false); sendUnread(unread,outbound); }else{ dao.putUnread(uid,4,eid,1,action.getOutline(),DateTimeUtil.currentTime(),false); } } dao.deleteFollower(eid); //删除所有参与者,补充和评论 dao.deleteAdd(eid); dao.deleteComment(eid); }
@Override protected void onBinaryData(InputStream is) throws IOException { // Simply echo the data to back to the client. WsOutbound outbound = getWsOutbound(); int i = is.read(); while (i != -1) { outbound.writeBinaryData(i); i = is.read(); } outbound.flush(); }
@Override protected void onTextData(Reader r) throws IOException { // Simply echo the data to back to the client. WsOutbound outbound = getWsOutbound(); int c = r.read(); while (c != -1) { outbound.writeTextData((char) c); c = r.read(); } outbound.flush(); }
@Override protected void onOpen(WsOutbound outbound) { connections.add(this); String message = String.format("* %s %s", nickname, "has joined."); broadcast(message); }
/** * Sends the given status on the given WebSocket connection and closes the * connection. * * @param outbound The outbound WebSocket connection to close. * @param guac_status The status to send. */ public void closeConnection(WsOutbound outbound, GuacamoleStatus guac_status) { try { byte[] message = Integer.toString(guac_status.getGuacamoleStatusCode()).getBytes("UTF-8"); outbound.close(guac_status.getWebSocketCode(), ByteBuffer.wrap(message)); } catch (IOException e) { logger.debug("Unable to close WebSocket tunnel.", e); } }
public void action14(ServerMsg message){ EventAddVo action = (EventAddVo) message.getAction(); int eid = action.getEid(); List<Integer> uids = dao.getAllFollowerUser(eid); for(int uid:uids){ WsOutbound outbound = userMap.get(uid); if(outbound != null){ UnreadVo unread = dao.putUnread(uid,0,eid,0,action.getContent(),DateTimeUtil.currentTime(),true); unread.setIsread(false); sendUnread(unread,outbound); }else{ dao.putUnread(uid,0,eid,0,action.getContent(),DateTimeUtil.currentTime(),false); } } }
@Override protected StreamInbound createWebSocketInbound(String subProtocol, HttpServletRequest request) { try{ final URI uri = new URI(request.getRequestURI()); return new MessageInbound() { @Override protected void onBinaryMessage(ByteBuffer arg0) throws IOException { } @Override protected void onTextMessage(CharBuffer message) throws IOException { l.onTextMessage(message); } @Override protected void onOpen(final WsOutbound outbound) { l.onOpen(new Connection() { @Override public URI getRequestUri() { return uri; } @Override public void send(CharSequence text) throws IOException { outbound.writeTextMessage(CharBuffer.wrap(text)); } }); } @Override protected void onClose(int status) { l.onClose(status); } private ConnectionListener l = handler.createConnectionListener(); }; } catch(URISyntaxException e){ throw new RuntimeException(e); } }
/** * Sends send the result of {@link Serializer#serialize(List)} to a destination. * * @param buffer the bytes to send. * @param destination The peer to send to. */ private void send(final byte[] buffer, final Object destination) { if (LOG.isTraceEnabled()) { LOG.trace("Sending from thread: id: " + Thread.currentThread().getName() + ", name: " + Thread.currentThread().getName()); } final WsOutbound outbound = ((MessageInbound) destination).getWsOutbound(); final ExecutorService executorService = connectionThreads.get(destination); // execute asynchronously to avoid slower clients from interfering with faster clients executorService.execute(new Runnable() { @Override public void run() { try { outbound.writeBinaryMessage(ByteBuffer.wrap(buffer)); } catch (final IOException e) { LOG.warn("Sending data to a client failed. Closing connection to this client."); try { outbound.close(1002, null); // CHECKSTYLE:OFF } catch (final IOException e1) { // Maybe the connection is already closed. This is no exceptional state but rather the // default in // this case. So it's safe to ignore this exception. } // CHECKSTYLE:ON connectionCloses((SynchronizeFXTomcatConnection) destination); } } }); }
public Snake(int id, WsOutbound outbound) { this.id = id; this.outbound = outbound; this.hexColor = SnakeWebSocketServlet.getRandomHexColor(); resetState(); }
@Override public void onOpen(WsOutbound out) { this.out = out; this.d.notifyOpen(); }
@Override protected void onOpen(final WsOutbound outbound) { parent.clientConnectionReady(this); }