Search in sources :

Example 1 with BinaryWebsocketMessage

use of websocket.drawboard.wsmessages.BinaryWebsocketMessage in project tomcat by apache.

the class Client method internalSendMessageAsync.

/**
     * Internally sends the messages asynchronously.
     * @param msg
     */
private void internalSendMessageAsync(AbstractWebsocketMessage msg) {
    try {
        if (msg instanceof StringWebsocketMessage) {
            StringWebsocketMessage sMsg = (StringWebsocketMessage) msg;
            async.sendText(sMsg.getString(), sendHandler);
        } else if (msg instanceof BinaryWebsocketMessage) {
            BinaryWebsocketMessage bMsg = (BinaryWebsocketMessage) msg;
            async.sendBinary(bMsg.getBytes(), sendHandler);
        } else if (msg instanceof CloseWebsocketMessage) {
            // Close the session.
            session.close();
        }
    } catch (IllegalStateException | IOException ex) {
    // Trying to write to the client when the session has
    // already been closed.
    // Ignore
    }
}
Also used : CloseWebsocketMessage(websocket.drawboard.wsmessages.CloseWebsocketMessage) StringWebsocketMessage(websocket.drawboard.wsmessages.StringWebsocketMessage) IOException(java.io.IOException) BinaryWebsocketMessage(websocket.drawboard.wsmessages.BinaryWebsocketMessage)

Example 2 with BinaryWebsocketMessage

use of websocket.drawboard.wsmessages.BinaryWebsocketMessage in project tomcat by apache.

the class Room method createAndAddPlayer.

/**
     * Creates a Player from the given Client and adds it to this room.
     *
     * @param client the client
     *
     * @return The newly created player
     */
public Player createAndAddPlayer(Client client) {
    if (players.size() >= MAX_PLAYER_COUNT) {
        throw new IllegalStateException("Maximum player count (" + MAX_PLAYER_COUNT + ") has been reached.");
    }
    Player p = new Player(this, client);
    // Broadcast to the other players that one player joined.
    broadcastRoomMessage(MessageType.PLAYER_CHANGED, "+");
    // Add the new player to the list.
    players.add(p);
    // If currently no Broadcast Timer Task is scheduled, then we need to create one.
    if (activeBroadcastTimerTask == null) {
        activeBroadcastTimerTask = createBroadcastTimerTask();
        drawmessageBroadcastTimer.schedule(activeBroadcastTimerTask, TIMER_DELAY, TIMER_DELAY);
    }
    // Send him the current number of players and the current room image.
    String content = String.valueOf(players.size());
    p.sendRoomMessage(MessageType.IMAGE_MESSAGE, content);
    // Store image as PNG
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    try {
        ImageIO.write(roomImage, "PNG", bout);
    } catch (IOException e) {
    /* Should never happen */
    }
    // Send the image as binary message.
    BinaryWebsocketMessage msg = new BinaryWebsocketMessage(ByteBuffer.wrap(bout.toByteArray()));
    p.getClient().sendMessage(msg);
    return p;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BinaryWebsocketMessage(websocket.drawboard.wsmessages.BinaryWebsocketMessage)

Aggregations

IOException (java.io.IOException)2 BinaryWebsocketMessage (websocket.drawboard.wsmessages.BinaryWebsocketMessage)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 CloseWebsocketMessage (websocket.drawboard.wsmessages.CloseWebsocketMessage)1 StringWebsocketMessage (websocket.drawboard.wsmessages.StringWebsocketMessage)1