use of websocket.drawboard.wsmessages.StringWebsocketMessage 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.StringWebsocketMessage in project tomcat by apache.
the class Client method sendMessage.
/**
* Sends the given message asynchronously to the client.
* If there is already a async sending in progress, then the message
* will be buffered and sent when possible.<br><br>
*
* This method can be called from multiple threads.
*
* @param msg The message to send
*/
public void sendMessage(AbstractWebsocketMessage msg) {
synchronized (messagesToSend) {
if (!isClosing) {
// Check if we have a Close message
if (msg instanceof CloseWebsocketMessage) {
isClosing = true;
}
if (isSendingMessage) {
// or length(of all messages) >= 1000000 bytes.
if (messagesToSend.size() >= 1000 || messagesToSendLength >= 1000000) {
isClosing = true;
// Discard the new message and close the session immediately.
CloseReason cr = new CloseReason(CloseCodes.VIOLATED_POLICY, "Send Buffer exceeded");
try {
// TODO: close() may block if the remote endpoint doesn't read the data
// (eventually there will be a TimeoutException). However, this method
// (sendMessage) is intended to run asynchronous code and shouldn't
// block. Otherwise it would temporarily stop processing of messages
// from other clients.
// Maybe call this method on another thread.
// Note that when this method is called, the RemoteEndpoint.Async
// is still in the process of sending data, so there probably should
// be another way to abort the Websocket connection.
// Ideally, there should be some abort() method that cancels the
// connection immediately...
session.close(cr);
} catch (IOException e) {
// Ignore
}
} else {
// to reduce TCP overhead (using ";" as separator).
if (msg instanceof StringWebsocketMessage && !messagesToSend.isEmpty() && messagesToSend.getLast() instanceof StringWebsocketMessage) {
StringWebsocketMessage ms = (StringWebsocketMessage) messagesToSend.removeLast();
messagesToSendLength -= calculateMessageLength(ms);
String concatenated = ms.getString() + ";" + ((StringWebsocketMessage) msg).getString();
msg = new StringWebsocketMessage(concatenated);
}
messagesToSend.add(msg);
messagesToSendLength += calculateMessageLength(msg);
}
} else {
isSendingMessage = true;
internalSendMessageAsync(msg);
}
}
}
}
use of websocket.drawboard.wsmessages.StringWebsocketMessage in project tomcat by apache.
the class DrawboardEndpoint method onOpen.
@Override
public void onOpen(Session session, EndpointConfig config) {
// Set maximum messages size to 10.000 bytes.
session.setMaxTextMessageBufferSize(10000);
session.addMessageHandler(stringHandler);
final Client client = new Client(session);
final Room room = getRoom(true);
room.invokeAndWait(new Runnable() {
@Override
public void run() {
try {
// Create a new Player and add it to the room.
try {
player = room.createAndAddPlayer(client);
} catch (IllegalStateException ex) {
// Probably the max. number of players has been
// reached.
client.sendMessage(new StringWebsocketMessage("0" + ex.getLocalizedMessage()));
// Close the connection.
client.close();
}
} catch (RuntimeException ex) {
log.error("Unexpected exception: " + ex.toString(), ex);
}
}
});
}
Aggregations