Exemple #1
0
  private void handleChat(final Message clientMsg) {
    Message broadcastMsg;

    if (clientMsg.SUBTYPE.equals("MSG")) {

      lastActivityTime = System.currentTimeMillis();

      ChatMsg chatMsg = new ChatMsg();
      broadcastMsg = clientMsg;
      // You can't trust nobody ;)
      chatMsg.MSG = clientMsg.CHAT_MSG.MSG.replace("<", "&lt;").replace("&", "&amp;");
      chatMsg.COLOR = (String) thisSession.getUserProperties().get("COLOR");
      chatMsg.FROM = (String) thisSession.getUserProperties().get("USER");

      ChatLog logMsg = new ChatLog();
      logMsg.setDate(new Date(lastActivityTime));
      logMsg.setUser(chatMsg.FROM);
      logMsg.setMessage(chatMsg.MSG);

      try {
        chatLogRepository.insert(logMsg);
      } catch (Exception e) {
        LOG.error(e.getMessage());
      }

      broadcastMsg.CHAT_MSG = chatMsg;
      broadcastMessage(broadcastMsg, true);
    }
  }
Exemple #2
0
 private void sendPong() {
   if (System.currentTimeMillis() - lastActivityTime > chatSessionTimeout * 1000) {
     httpSession.invalidate();
     return;
   }
   Message pongMsg = new Message();
   pongMsg.TYPE = "PONG";
   sendMessage(pongMsg);
 }
Exemple #3
0
  private void unjoinChat() {
    if (thisSession.getUserProperties().containsKey("USER")) {
      LOG.debug("unjoinChat(): " + thisSession.getUserProperties().get("USER"));

      sessionService.removeOnSessionDestroyedListener(callback);

      if (isHttpSessionValid) {
        int sessionIdleTime =
            (int) ((System.currentTimeMillis() - httpSession.getLastAccessedTime()) / 1000);
        LOG.debug("Max idle timeout: " + (sessionIdleTime + defaultSessionTimeout));
        httpSession.setMaxInactiveInterval(sessionIdleTime + defaultSessionTimeout);
      }

      int userNb = usersLoggedIn.decrementAndGet();

      Message infoMsg = new Message();

      infoMsg.TYPE = "INFO";
      infoMsg.SUBTYPE = "JOIN";
      infoMsg.INFO_MSG = thisSession.getUserProperties().get("USER") + " has left the building";
      infoMsg.STATS_MSG = userNb + " User" + (userNb > 1 ? "s " : " ") + "online!";
      infoMsg.USER_LIST = buildUserList(false);

      thisSession.getUserProperties().clear();

      broadcastMessage(infoMsg, false);
    }
  }
Exemple #4
0
  private void joinChat() {
    String userColor;

    sessionService.addOnSessionDestroyedListener(callback);

    defaultSessionTimeout = httpSession.getMaxInactiveInterval();
    httpSession.setMaxInactiveInterval(0);
    lastActivityTime = System.currentTimeMillis();

    String username = ((User) authToken.getPrincipal()).getUsername();
    LOG.debug("joinChat() user: "******"USER", username);

    int userNb = usersLoggedIn.incrementAndGet();
    // If a user is active more than once, give him the same color:
    if (userColorMap.containsKey(username)) {
      userColor = userColorMap.get(username);
    } else {
      userColor = PEER_COLORS[userNb % PEER_COLOR_NB];
      userColorMap.put(username, userColor);
    }

    thisSession.getUserProperties().put("COLOR", userColor);

    Message joinMsg = new Message();
    joinMsg.TYPE = "JOIN";
    joinMsg.SUBTYPE = "JOIN";
    joinMsg.USER_LIST = buildUserList(true);
    joinMsg.STATS_MSG = userNb + " User" + (userNb > 1 ? "s " : " ") + "online!";

    sendMessage(joinMsg);

    Message infoMsg = new Message();
    infoMsg.TYPE = "INFO";
    infoMsg.SUBTYPE = "JOIN";
    infoMsg.INFO_MSG = username + " has entered the building";
    infoMsg.STATS_MSG = userNb + " User" + (userNb > 1 ? "s " : " ") + "online!";
    infoMsg.USER_LIST = buildUserList(true);

    broadcastMessage(infoMsg, false);
  }