Search in sources :

Example 1 with CloseWebsocketMessage

use of websocket.drawboard.wsmessages.CloseWebsocketMessage 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 CloseWebsocketMessage

use of websocket.drawboard.wsmessages.CloseWebsocketMessage 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);
            }
        }
    }
}
Also used : CloseWebsocketMessage(websocket.drawboard.wsmessages.CloseWebsocketMessage) StringWebsocketMessage(websocket.drawboard.wsmessages.StringWebsocketMessage) CloseReason(javax.websocket.CloseReason) IOException(java.io.IOException)

Aggregations

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