org.apache.catalina.websocket.WsOutbound Java Examples

The following examples show how to use org.apache.catalina.websocket.WsOutbound. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ActionListener.java    From ALLGO with Apache License 2.0 7 votes vote down vote up
@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);
			}
		}
	}
}
 
Example #2
Source File: ActionListener.java    From ALLGO with Apache License 2.0 6 votes vote down vote up
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);
	
}
 
Example #3
Source File: ActionListener.java    From ALLGO with Apache License 2.0 6 votes vote down vote up
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);
	}
}
 
Example #4
Source File: ActionListener.java    From ALLGO with Apache License 2.0 6 votes vote down vote up
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();
	}
	
}
 
Example #5
Source File: PushServlet.java    From ALLGO with Apache License 2.0 6 votes vote down vote up
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();
		}
	}
}
 
Example #6
Source File: PushServlet.java    From ALLGO with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #7
Source File: EchoStream.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #8
Source File: SynchronizeFXTomcatChannel.java    From SynchronizeFX with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 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);
            }
        }
    });
}
 
Example #9
Source File: ActionListener.java    From ALLGO with Apache License 2.0 5 votes vote down vote up
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);
		}
	}
}
 
Example #10
Source File: ChatWebSocketServlet.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
protected void onOpen(WsOutbound outbound) {
    connections.add(this);
    String message = String.format("* %s %s",
            nickname, "has joined.");
    broadcast(message);
}
 
Example #11
Source File: EchoStream.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #12
Source File: EchoStream.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #13
Source File: ChatWebSocketServlet.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
protected void onOpen(WsOutbound outbound) {
    connections.add(this);
    String message = String.format("* %s %s",
            nickname, "has joined.");
    broadcast(message);
}
 
Example #14
Source File: EchoStream.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #15
Source File: EchoStream.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #16
Source File: ChatWebSocketServlet.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
protected void onOpen(WsOutbound outbound) {
    connections.add(this);
    String message = String.format("* %s %s",
            nickname, "has joined.");
    broadcast(message);
}
 
Example #17
Source File: EchoStream.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #18
Source File: Snake.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
public Snake(int id, WsOutbound outbound) {
    this.id = id;
    this.outbound = outbound;
    this.hexColor = SnakeWebSocketServlet.getRandomHexColor();
    resetState();
}
 
Example #19
Source File: Snake.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
public Snake(int id, WsOutbound outbound) {
    this.id = id;
    this.outbound = outbound;
    this.hexColor = SnakeWebSocketServlet.getRandomHexColor();
    resetState();
}
 
Example #20
Source File: Snake.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
public Snake(int id, WsOutbound outbound) {
    this.id = id;
    this.outbound = outbound;
    this.hexColor = SnakeWebSocketServlet.getRandomHexColor();
    resetState();
}
 
Example #21
Source File: SynchronizeFXTomcatConnection.java    From SynchronizeFX with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
protected void onOpen(final WsOutbound outbound) {
    parent.clientConnectionReady(this);
}
 
Example #22
Source File: GuacamoleWebSocketTunnelServlet.java    From guacamole-client with Apache License 2.0 3 votes vote down vote up
/**
 * Sends the given Guacamole and WebSocket numeric status
 * on the given WebSocket connection and closes the
 * connection.
 *
 * @param outbound
 *     The outbound WebSocket connection to close.
 *
 * @param guacamoleStatusCode
 *     The status to send.
 *
 * @param webSocketCode
 *     The numeric WebSocket status code to send.
 */
private void closeConnection(WsOutbound outbound, int guacamoleStatusCode,
        int webSocketCode) {

    try {
        byte[] message = Integer.toString(guacamoleStatusCode).getBytes("UTF-8");
        outbound.close(webSocketCode, ByteBuffer.wrap(message));
    }
    catch (IOException e) {
        logger.debug("Unable to close WebSocket tunnel.", e);
    }

}
 
Example #23
Source File: GuacamoleWebSocketTunnelServlet.java    From guacamole-client with Apache License 2.0 3 votes vote down vote up
/**
 * Sends the given status on the given WebSocket connection
 * and closes the connection.
 *
 * @param outbound
 *     The outbound WebSocket connection to close.
 *
 * @param guacStatus
 *     The status to send.
 */
private void closeConnection(WsOutbound outbound,
        GuacamoleStatus guacStatus) {

    closeConnection(outbound, guacStatus.getGuacamoleStatusCode(),
            guacStatus.getWebSocketCode());

}
 
Example #24
Source File: GuacamoleWebSocketTunnelServlet.java    From guacamole-client with Apache License 2.0 3 votes vote down vote up
/**
 * Sends the given Guacamole and WebSocket numeric status
 * on the given WebSocket connection and closes the
 * connection.
 *
 * @param outbound
 *     The outbound WebSocket connection to close.
 *
 * @param guacamoleStatusCode
 *     The status to send.
 *
 * @param webSocketCode
 *     The numeric WebSocket status code to send.
 */
private void closeConnection(WsOutbound outbound, int guacamoleStatusCode,
        int webSocketCode) {

    try {
        byte[] message = Integer.toString(guacamoleStatusCode).getBytes("UTF-8");
        outbound.close(webSocketCode, ByteBuffer.wrap(message));
    }
    catch (IOException e) {
        logger.debug("Unable to close WebSocket tunnel.", e);
    }

}
 
Example #25
Source File: GuacamoleWebSocketTunnelServlet.java    From guacamole-client with Apache License 2.0 3 votes vote down vote up
/**
 * Sends the given status on the given WebSocket connection
 * and closes the connection.
 *
 * @param outbound
 *     The outbound WebSocket connection to close.
 *
 * @param guacStatus
 *     The status to send.
 */
private void closeConnection(WsOutbound outbound,
        GuacamoleStatus guacStatus) {

    closeConnection(outbound, guacStatus.getGuacamoleStatusCode(),
            guacStatus.getWebSocketCode());

}