Пример #1
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);
    }
  }
Пример #2
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);
  }