Example usage for javax.websocket Session getUserProperties

List of usage examples for javax.websocket Session getUserProperties

Introduction

In this page you can find the example usage for javax.websocket Session getUserProperties.

Prototype

Map<String, Object> getUserProperties();

Source Link

Usage

From source file:org.damcode.web.c4webserver.Server.java

private synchronized void doChatMessage(MessageBean message, Session session) {
    String name = (String) session.getUserProperties().get("name");
    if (name == null) {
        name = "unknown";
    }/*from  ww w.  j  a va  2 s.  c  o  m*/

    String data = message.getData().replace("\\", "");
    data = data.replace("\"", "\\\"");

    for (Session s : session.getOpenSessions()) {
        try {
            if (s == session)
                continue;

            if (s.getUserProperties().get("gameid").equals(session.getUserProperties().get("gameid"))) { // only send messages to people in same "game"
                s.getBasicRemote().sendText(Utils.assembleChatMessage(name, data));

            } else if ((Boolean) s.getUserProperties().get("lobby") == true
                    && (Boolean) session.getUserProperties().get("lobby") == true) {
                if (!session.getUserProperties().get("gameid").equals("lobby")) {
                    s.getBasicRemote().sendText(Utils.assembleChatMessage(name, data, "playing"));
                } else {
                    s.getBasicRemote().sendText(Utils.assembleChatMessage(name, data, "lobby"));
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:org.damcode.web.c4webserver.Server.java

private GameController joinGame(Session s, MessageBean m) throws IOException {
    synchronized (waitingGames) {
        for (GameController g : waitingGames) {
            if (g.getId().equals(UUID.fromString(m.getData()))) {
                printSysOut("found game match");
                g.addPlayer(s);//  ww w . ja v a2 s .co m
                s.getUserProperties().put("gameid", g.getId());
                activeGames.add(g);
                g.gameStatus = GameController.GAME_ACTIVE;

                printSysOut("goplayer: " + g.getOpponent(s).getName());
                g.getPlayer(s).sendDataMessage(MessageBean.MSG_PLAYER_SELECT,
                        String.valueOf(g.getOpponent(s).imageId));
                printSysOut("send disable image id: " + String.valueOf(g.getOpponent(s).imageId));

                return g;
            } else {
                printSysOut("NO GAME MATCH FOUND: " + m.getData());
            }
        }
    }
    return null;
}

From source file:org.damcode.web.c4webserver.Server.java

private void quitGame(Session s, int reason) {
    printSysOut("got quitgame from : " + s.getId());
    GameController g = getGame(s);//from  w  w w  .  j a v  a2  s . com

    // TODO : fix quitgame to destroy any games attached when players quit!
    if (g == null) {
        printSysOut("quitgame game was null");
        return;
    }

    Player p = g.getPlayer(s);
    Player opponent = g.getOpponent(s);

    if (opponent != null) {
        // could still retrieve game if player reconnects
        // destroy only the player object from the game;
        // TODO implement this later, for now just close the game object

        opponent.sendDataMessage(reason, "quit");
        printSysOut("Quitting player removed: " + (getGame(s).players.remove(p)));

    } else { // no opponent, shutdown game;
        if (g.gameStatus == GameController.GAME_ACTIVE || g.gameStatus == GameController.GAME_STARTED
                || g.gameStatus == GameController.GAME_ENDED) {
            printSysOut("Last player removed: " + (getGame(s).players.remove(p)));
            printSysOut("quitgame: removed active game");
            activeGames.remove(g);

        } else if (g.gameStatus == GameController.GAME_WAITING) {
            printSysOut("Last waiting player removed: " + (getGame(s).players.remove(p)));
            printSysOut("quitgame: removed waiting game");
            waitingGames.remove(g);
        }
    }

    printSysOut("players size: " + g.players.size());
    s.getUserProperties().remove("game");
    s.getUserProperties().put("gameid", "lobby");

}

From source file:org.wso2.carbon.identity.agent.outbound.server.UserStoreServerEndpoint.java

@OnOpen
public void onOpen(@PathParam("node") String node, Session session) {
    handleSession(getAccessTokenFromUserProperties(session.getUserProperties()), node, session);
}

From source file:org.wso2.carbon.identity.agent.outbound.server.UserStoreServerEndpoint.java

@OnClose
public void onClose(@PathParam("node") String node, CloseReason closeReason, Session session) {

    Boolean isOnCloseNeeded = this.isOnCloseNeededMap.get(session.getId());
    if (isOnCloseNeeded != null && !isOnCloseNeeded) {
        isOnCloseNeededMap.remove(session.getId());
        return;//from   www  .j a  va 2 s . c o  m
    }

    TokenMgtDao tokenMgtDao = new TokenMgtDao();
    AccessToken accessToken = tokenMgtDao
            .getAccessToken(getAccessTokenFromUserProperties(session.getUserProperties()));

    LOGGER.info("Connection close triggered with status code : " + closeReason.getCloseCode().getCode()
            + " On reason " + closeReason.getReasonPhrase() + " from " + node + " in tenant "
            + accessToken.getTenant());
    if (accessToken != null) {
        serverHandler.removeSession(accessToken.getTenant(), accessToken.getDomain(), session);
        AgentMgtDao agentMgtDao = new AgentMgtDao();
        agentMgtDao.updateConnection(accessToken.getId(), node, serverNode,
                UserStoreConstants.CLIENT_CONNECTION_STATUS_CONNECTION_FAILED);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Connection close for tenant: " + accessToken.getTenant());
        }
        String msg = "Client : " + node + " from " + accessToken.getTenant()
                + " disconnected from server node: " + serverNode;
        LOGGER.info(msg);
    }
}