Ejemplo n.º 1
0
 /** 给特定的在线用户发送消息 */
 public void sendMessageToUsers(ArrayList<Long> userIdList, TextMessage message) {
   if (userIdList == null) {
     return;
   }
   int listSize = userIdList.size();
   if (listSize == 0) {
     return;
   }
   int i = 0;
   for (WebSocketSession user : users) {
     try {
       if (user.getAttributes().get("USER_ID") != null) {
         if (user.isOpen() && userIdList.contains(user.getAttributes().get("USER_ID"))) {
           user.sendMessage(message);
           i++;
           if (listSize == i) {
             break;
           }
         }
       }
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
 }
 /** 会话抛出异常 */
 @Override
 public void handleTransportError(WebSocketSession session, Throwable arg1) throws Exception {
   // TODO Auto-generated method stub
   if (session.isOpen()) {
     session.close();
   }
   WebSocketCache.removeMemberSession(session);
 }
Ejemplo n.º 3
0
 @Override
 public void handleTransportError(WebSocketSession session, Throwable exception)
     throws IOException {
   if (session.isOpen()) {
     session.close();
   }
   users.remove(session);
 }
	@Override
	public void afterConnectionEstablished(WebSocketSession session) throws Exception {
		// WebSocketHandlerDecorator could close the session
		if (!session.isOpen()) {
			return;
		}
		this.stats.incrementSessionCount(session);
		session = new ConcurrentWebSocketSessionDecorator(session, getSendTimeLimit(), getSendBufferSizeLimit());
		this.sessions.put(session.getId(), new WebSocketSessionHolder(session));
		findProtocolHandler(session).afterSessionStarted(session, this.clientInboundChannel);
	}
Ejemplo n.º 5
0
 /** 给所有在线用户发送消息 */
 public void sendMessageToUsers(TextMessage message) {
   for (WebSocketSession user : users) {
     try {
       if (user.isOpen()) {
         user.sendMessage(message);
       }
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
 }
Ejemplo n.º 6
0
  private void sendMessage(final WebSocketSession session, final TextMessage textMessage) {

    this.taskExecutor.execute(
        () -> {
          if (session.isOpen()) {
            try {
              session.sendMessage(textMessage);
            } catch (IOException e) {
              logger.error("sendMessage to session", e);
            }
          }
        });
  }
 /** 消息传输错误处理 */
 public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
   if (session.isOpen()) {
     session.close();
   }
   Iterator<Entry<Long, WebSocketSession>> it = userSocketSessionMap.entrySet().iterator();
   // 移除Socket会话
   while (it.hasNext()) {
     Entry<Long, WebSocketSession> entry = it.next();
     if (entry.getValue().getId().equals(session.getId())) {
       userSocketSessionMap.remove(entry.getKey());
       System.out.println("Socket会话已经移除:用户ID" + entry.getKey());
       break;
     }
   }
 }
 /**
  * 给某个用户发送消息
  *
  * @param userName
  * @param message
  * @throws IOException
  */
 public void sendMessageToUser(Long uid, TextMessage message) throws IOException {
   WebSocketSession session = userSocketSessionMap.get(uid);
   if (session != null && session.isOpen()) {
     session.sendMessage(message);
   }
 }