Example usage for javax.websocket Session getId

List of usage examples for javax.websocket Session getId

Introduction

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

Prototype

String getId();

Source Link

Document

Provides a unique identifier for the session.

Usage

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

@OnClose
public void onClose(Session s) {
    printSysOut("Closing session: " + s.getId());

    GameController g = getGame(s);//from   ww w.j av  a2s  .  c o  m
    if (g != null) {

        Player p = g.getPlayer(s);
        if (g.gameStatus == GameController.GAME_WAITING) {
            waitingGames.remove(g);
            printSysOut("waiting game removed");
        }

        if (g.gameStatus == GameController.GAME_ACTIVE || g.gameStatus == GameController.GAME_STARTED
                || g.gameStatus == GameController.GAME_ENDED) {

            if (g.players.size() < 2) {
                activeGames.remove(g);
                printSysOut("active game removed");
            } else {
                quitGame(s, MessageBean.MSG_PLAYER_DISC);
                printSysOut("Quitting session, telling opponent");
            }

        }
    }
}

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);//  w w  w .j ava  2s .c  o m

    // 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.dicen.recolnat.services.core.MessageProcessorThread.java

private void sendMessage(String message, Session session) {
    // If not synchronized sometimes frames can get randomly lost. No idea why. Behavior appears in Tomcat war but not in stand-alone jar.
    // Randomness is more frequent if not synchronized.
    // Randomness seems to come from using Async, so let's stick with Basic for now.
    if (log.isDebugEnabled()) {
        log.debug("Sending message " + message + " to session " + session.getId());
    }//  www  .  j av a2  s .  co  m
    synchronized (session) {
        try {
            session.getBasicRemote().sendText(message);
        } catch (IOException ex) {
            log.error("Could not send message to client", ex);
        }
    }
}

From source file:org.dicen.recolnat.services.core.MessageProcessorThread.java

private boolean subscribe(Session session, String entityId) {
    ColaboratorySocket.mapAccessLock.lock();
    try {//w  w  w  .ja  v a 2s.co  m
        ColaboratorySocket.sessionIdToResources.put(session.getId(), entityId);
        ColaboratorySocket.resourceToSessions.put(entityId, session.getId());
    } finally {
        ColaboratorySocket.mapAccessLock.unlock();
    }
    return true;
}

From source file:org.dicen.recolnat.services.core.MessageProcessorThread.java

private boolean unsubscribe(Session session, String entityId) {
    ColaboratorySocket.mapAccessLock.lock();
    try {//  w w  w . j a v  a 2  s  .  c om
        Collection mapping = (Collection) ColaboratorySocket.resourceToSessions.get(entityId);
        Iterator<String> itSess = mapping.iterator();
        while (itSess.hasNext()) {
            String sessionId = itSess.next();
            if (sessionId.equals(session.getId())) {
                itSess.remove();
            }
        }

        mapping = (Collection) ColaboratorySocket.sessionIdToResources.get(session.getId());
        Iterator<String> itResources = mapping.iterator();
        while (itResources.hasNext()) {
            String resource = itResources.next();
            if (resource.equals((entityId))) {
                itResources.remove();
            }
        }
    } finally {
        ColaboratorySocket.mapAccessLock.unlock();
    }
    return true;
}

From source file:org.duniter.elasticsearch.websocket.WebSocketChangesEndPoint.java

@OnOpen
public void onOpen(Session session) {
    log.debug("Connected ... " + session.getId());
    this.session = session;
    this.sources = null;
    ChangeService.registerListener(this);
}

From source file:org.erstudio.guper.websocket.GuperWSEndpoint.java

@OnMessage
public void onWebSocketMessage(JsonObject json, Session session)
        throws WebSocketMessageException, MessageException, LocationException, IOException, EncodeException {
    System.out.println("onWebSocketMessage " + session.getId());

    if (!json.containsKey("type")) {
        throw new WebSocketMessageException("Type is undefined");
    }//from w  ww  .ja v  a 2  s .c o  m

    String type = json.getString("type");
    if (StringUtils.isBlank(type)) {
        throw new WebSocketMessageException("Empty message type");
    }

    if (!json.containsKey("message")) {
        throw new WebSocketMessageException("Message is undefined");
    }

    String message = json.get("message").toString();
    if (StringUtils.isBlank(message)) {
        throw new WebSocketMessageException("Empty message");
    }

    switch (type) {
    case "SEND_MESSAGE": {
        try {
            Message m = mapper.readValue(message, Message.class);
            onSendMessage(m, session);
        } catch (IOException ex) {
            throw new WebSocketMessageException("Incorrect Message format: " + message);
        }
    }
        break;

    case "CHANGE_LOCATION": {
        try {
            Location l = mapper.readValue(message, Location.class);
            onChangeLocation(l, session);
        } catch (IOException ex) {
            throw new WebSocketMessageException("Incorrect Location format: " + message);
        }
    }
        break;

    default:
        throw new WebSocketMessageException("Incorrect message type: " + type);
    }

}

From source file:org.erstudio.guper.websocket.GuperWSEndpoint.java

@OnOpen
public void onOpen(Session peer) throws IOException, EncodeException {
    System.out.println("onOpen " + peer.getId());
    userService.connectUser(peer);//from   w ww.  j a va2s .  c o m
}

From source file:org.erstudio.guper.websocket.GuperWSEndpoint.java

@OnClose
public void onClose(Session peer) {
    System.out.println("onClose " + peer.getId());
    userService.disconnectUser(peer);
}

From source file:org.wso2.carbon.device.mgt.analytics.wsproxy.inbound.SubscriptionEndpoint.java

/**
 * Web socket onOpen - When client sends a message
 *
 * @param session         - Users registered session.
 *//*from   w  ww  .  j  av  a  2s .  co m*/
void onOpen(Session session) {
    if (log.isDebugEnabled()) {
        log.debug("WebSocket opened, for Session id: " + session.getId());
    }

    AnalyticsConfiguration analyticsConfig = AnalyticsConfiguration.getInstance();
    ArrayList<String> publisherGroups = DataPublisherUtil
            .getEndpointGroups(analyticsConfig.getAnalyticsPublisherUrl());
    List<AnalyticsClient> analyticsClients = new ArrayList<>();
    for (String publisherURLGroup : publisherGroups) {
        try {
            String[] endpoints = DataPublisherUtil.getEndpoints(publisherURLGroup);
            for (String endpoint : endpoints) {
                try {
                    endpoint = endpoint.trim();
                    if (!endpoint.endsWith("/")) {
                        endpoint += "/";
                    }
                    endpoint += session.getRequestURI().getSchemeSpecificPart()
                            .replace("secured-websocket-proxy", "");
                    AnalyticsClient analyticsClient = new AnalyticsClient(session, new URI(endpoint));
                    analyticsClients.add(analyticsClient);
                } catch (URISyntaxException e) {
                    log.error("Unable to create URL from: " + endpoint, e);
                } catch (WSProxyException e) {
                    log.error("Unable to create WS client for: " + endpoint, e);
                }
            }
        } catch (DataEndpointConfigurationException e) {
            log.error("Unable to obtain endpoints from receiverURLGroup: " + publisherURLGroup, e);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Configured " + analyticsClients.size() + " analytics clients for Session id: "
                + session.getId());
    }
    analyticsClientsMap.put(session.getId(), analyticsClients);
}