Example usage for javax.websocket Session equals

List of usage examples for javax.websocket Session equals

Introduction

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

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:org.sample.whiteboard.Whiteboard.java

@OnMessage
public void broadcastFigure(Figure figure, Session session) throws IOException, EncodeException {
    System.out.println("boradcastFigure: " + figure);
    for (Session peer : peers) {
        if (!peer.equals(session)) {
            peer.getBasicRemote().sendObject(figure);
        }//from  ww  w. ja  v  a 2  s . co  m
    }
}

From source file:org.sample.whiteboard.Whiteboard.java

@OnMessage
public void broadcastSnapshot(ByteBuffer data, Session session) throws IOException {
    System.out.println("broadcastBinary: " + data);
    for (Session peer : peers) {
        if (!peer.equals(session)) {
            peer.getBasicRemote().sendBinary(data);
        }/*from www.  j  a  v a  2 s .c  o  m*/
    }
}

From source file:com.josue.ws.web.server.MessageDispatcher.java

private void sendAllExceptSender(MessageRequestWrapper wrapper) {
    Iterator<Session> sessions = store.getSessions(wrapper.getMap());
    while (sessions.hasNext()) {
        Session onlineSession = sessions.next();
        if (!onlineSession.equals(wrapper.getSession())) {
            onlineSession.getAsyncRemote().sendText(wrapper.getOriginalMessage());
        }// w w w  .  j a v a 2 s .  c  o m
    }
}

From source file:feedme.controller.SocketServer.java

/**
 * Method called when a connection is closed
 * *///from  w  w w .  java2  s.c om
@OnClose
public void onClose(Session session) {

    System.out.println("Session " + session.getId() + " has ended");

    // Getting the client name that exited

    for (Iterator<Map.Entry<String, Session>> it = nameSessionPair.entrySet().iterator(); it.hasNext();) {
        Map.Entry<String, Session> e = it.next();
        if (session.equals(e.getValue())) {
            it.remove();
        }
    }
    // removing the session from sessions list
    sessions.remove(session);

    System.out.println("Sessions size" + sessions.size());
    for (Iterator<Map.Entry<String, Session>> it = nameSessionPair.entrySet().iterator(); it.hasNext();) {
        Map.Entry<String, Session> e = it.next();
        System.out.println("rest id " + e.getKey() + " rest_session_id " + e.getValue().getId().toString());
    }

}

From source file:io.hops.hopsworks.api.zeppelin.socket.NotebookServerImpl.java

private void broadcastExcept(String noteId, Message m, Session exclude) {
    synchronized (noteSocketMap) {
        broadcastToWatchers(noteId, StringUtils.EMPTY, m);
        List<Session> socketLists = noteSocketMap.get(noteId);
        if (socketLists == null || socketLists.isEmpty()) {
            return;
        }//from w w  w  . j a v a2 s  .  c  o  m
        LOG.log(Level.FINE, "SEND >> {0}", m.op);
        for (Session conn : socketLists) {
            if (exclude.equals(conn)) {
                continue;
            }
            try {
                sendMsg(conn, serializeMessage(m));
            } catch (IOException ex) {
                LOG.log(Level.SEVERE, "Unable to send message " + m, ex);
            }
        }
    }
}

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

@OnOpen
public void onOpen(Session session, @PathParam("id") String gameId) throws IOException {
    printSysOut("new session: " + session.getId() + ", game id: " + gameId + "from ip: ");
    session.getUserProperties().put("gameid", gameId);
    session.getUserProperties().put("lobby", true);

    String connectedPlayers = "";
    int pCount = 0;
    for (Session s : session.getOpenSessions()) {
        if (!s.equals(session)) {
            String name = (String) s.getUserProperties().get("name");
            connectedPlayers += name + ", ";
            pCount++;/*  w ww .  j a v a  2s.c o m*/
        }
    }
    if (pCount > 0) {
        printSysOut("Connected Players: " + connectedPlayers);
        session.getBasicRemote()
                .sendText(Utils.assembleChatMessage("Server", connectedPlayers, "other players"));
    }

    for (GameController gc : waitingGames) {
        sendGameAvailMessage(session, gc.getId().toString(), gc.players.get(0));
    }
}

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

@OnMessage
public void onMessage(MessageBean message, Session session, @PathParam("id") String gameId) {
    printSysOut("Current active games: " + activeGames.size());
    printSysOut("Current waiting games: " + waitingGames.size());
    String players = "";
    int pCount = 0;
    for (Session s : session.getOpenSessions()) {
        if (!s.equals(session)) {
            String name = (String) s.getUserProperties().get("name");
            players += name;/*from   w w  w  . j  a v a 2  s .  co  m*/
            pCount++;
        }
    }
    printSysOut("Connected Players: " + players + " / " + pCount);

    try {

        processMessage(message, session);

    } catch (IOException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    }

}

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

private void processMessage(MessageBean m, Session s) throws IOException {
    Player p, o;//  www  .ja v  a  2 s  .  c  o  m
    GameController g;

    switch (m.getCommand()) {

    case MessageBean.MSG_CHAT:
        doChatMessage(m, s);
        break;

    case MessageBean.MSG_PASS_MOVE_TOKEN:
        getGame(s).passMoveToken(s);
        break;

    case MessageBean.MSG_MOVE_ACTION:
        int c = Integer.parseInt(m.getData());
        getGame(s).dropPiece(s, c);
        printSysOut("player move: " + m.getData());
        break;

    case MessageBean.MSG_START_GAME:
        if (isAlreadyPlaying(s)) {
            break;
        }
        GameController game = new GameController();
        game.addPlayer(s);
        s.getUserProperties().put("gameid", game.getId());
        waitingGames.add(game);
        game.gameStatus = GameController.GAME_WAITING;

        s.getBasicRemote().sendText(Utils.assembleChatMessage("Server",
                "ok, started game, waiting for players to join.", "server"));

        game.getPlayer(s).sendDataMessage(MessageBean.MSG_START_GAME, game.getId().toString());
        //sendDataMessage(s, , );
        printSysOut("start game: " + m.getData());
        break;

    case MessageBean.MSG_PLAYER_NAME:
        String name = m.getData();
        s.getUserProperties().put("name", name);
        printSysOut("user with name: " + name);
        for (Session ss : s.getOpenSessions()) {
            if (ss.equals(s))
                continue;
            ss.getBasicRemote()
                    .sendText(Utils.assembleChatMessage("Server", "Player join lobby: " + name, "server"));
        }
        break;

    case MessageBean.MSG_JOIN_GAME:
        if (isAlreadyPlaying(s)) {
            break;
        }

        printSysOut("joingame: " + m.getData());
        g = joinGame(s, m);
        if (g != null) {
            try {
                waitingGames.remove(g);
            } catch (Exception e) {
                printSysOut("JOIN FAILED");
            }
        } else {
            printSysOut("NO GAME MATCH FOUND: " + m.getData());
            Utils.sendDataMessage(MessageBean.MSG_PLAYER_QUIT, "Game has expired!", s);
        }
        break;

    case MessageBean.MSG_JOIN_LOBBY_CHAT:
        s.getUserProperties().put("lobby", Boolean.parseBoolean(m.getData()));
        break;

    case MessageBean.MSG_GAME_AVAILABLE:
        if (isAlreadyPlaying(s)) {
            break;
        }
        synchronized (waitingGames) {
            if (waitingGames.isEmpty()) {
                s.getBasicRemote().sendText(
                        Utils.assembleChatMessage("Server", "No games available, try starting one!", "server"));
            }
            for (GameController gc : waitingGames) {
                sendGameAvailMessage(s, gc.getId().toString(), gc.players.get(0));
            }
        }
        break;

    case MessageBean.MSG_PLAYER_READY:
        g = getGame(s);
        if (g == null || g.gameStatus == GameController.GAME_STARTED)
            break;
        printSysOut("gamestate = " + g.gameStatus);
        p = g.getPlayer(s);
        o = g.getOpponent(s);

        p.ready = true;

        if (g.readyCheck()) {
            p.sendDataMessage(MessageBean.MSG_PLAYER_READY, "rdy");
            o.sendDataMessage(MessageBean.MSG_PLAYER_READY, "rdy");
            g.start();
        }
        break;

    case MessageBean.MSG_PLAYER_QUIT:
        quitGame(s, MessageBean.MSG_PLAYER_QUIT);
        break;

    case MessageBean.MSG_PLAYER_SELECT:
        g = getGame(s);
        if (g == null)
            break;
        p = g.getPlayer(s);
        p.imageId = Integer.parseInt(m.getData());
        o = g.getOpponent(s);
        if (o != null) {
            printSysOut("SENT DISABLE MESSAGE: " + p.imageId + " to: " + o.session.getId());
            o.sendDataMessage(MessageBean.MSG_PLAYER_SELECT, m.getData());
        }
        break;

    default:
        break;

    }

}