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
}
}
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;
}
Aggregations