Search in sources :

Example 1 with UTF8Output

use of io.undertow.websockets.core.UTF8Output in project undertow by undertow-io.

the class FrameHandler method onFullCloseMessage.

@Override
protected void onFullCloseMessage(final WebSocketChannel channel, final BufferedBinaryMessage message) {
    if (session.isSessionClosed()) {
        // we have already handled this when we sent the close frame
        message.getData().free();
        return;
    }
    final Pooled<ByteBuffer[]> pooled = message.getData();
    final ByteBuffer singleBuffer = toBuffer(pooled.getResource());
    final ByteBuffer toSend = singleBuffer.duplicate();
    // send the close immediatly
    WebSockets.sendClose(toSend, channel, null);
    session.getContainer().invokeEndpointMethod(executor, new Runnable() {

        @Override
        public void run() {
            try {
                if (singleBuffer.remaining() > 1) {
                    final CloseReason.CloseCode code = CloseReason.CloseCodes.getCloseCode(singleBuffer.getShort());
                    final String reasonPhrase = singleBuffer.remaining() > 1 ? new UTF8Output(singleBuffer).extract() : null;
                    session.closeInternal(new CloseReason(code, reasonPhrase));
                } else {
                    session.closeInternal(new CloseReason(CloseReason.CloseCodes.NO_STATUS_CODE, null));
                }
            } catch (IOException e) {
                invokeOnError(e);
            } finally {
                pooled.close();
            }
        }
    });
}
Also used : CloseReason(javax.websocket.CloseReason) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) UTF8Output(io.undertow.websockets.core.UTF8Output)

Aggregations

UTF8Output (io.undertow.websockets.core.UTF8Output)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 CloseReason (javax.websocket.CloseReason)1