Search in sources :

Example 1 with CloseMessage

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

the class UndertowSession method closeInternal.

public void closeInternal(CloseReason closeReason) throws IOException {
    if (closed.compareAndSet(false, true)) {
        try {
            try {
                if (!webSocketChannel.isCloseFrameReceived() && !webSocketChannel.isCloseFrameSent()) {
                    //will deal with sending back the reason message
                    if (closeReason == null || closeReason.getCloseCode().getCode() == CloseReason.CloseCodes.NO_STATUS_CODE.getCode()) {
                        webSocketChannel.sendClose();
                    } else {
                        WebSockets.sendClose(new CloseMessage(closeReason.getCloseCode().getCode(), closeReason.getReasonPhrase()).toByteBuffer(), webSocketChannel, null);
                    }
                }
            } finally {
                try {
                    String reason = null;
                    CloseReason.CloseCode code = CloseReason.CloseCodes.NO_STATUS_CODE;
                    if (webSocketChannel.getCloseCode() != -1) {
                        reason = webSocketChannel.getCloseReason();
                        code = CloseReason.CloseCodes.getCloseCode(webSocketChannel.getCloseCode());
                    } else if (closeReason != null) {
                        reason = closeReason.getReasonPhrase();
                        code = closeReason.getCloseCode();
                    }
                    //we need to really clean up the close behaviour in the next spec
                    if (!webSocketChannel.isCloseInitiatedByRemotePeer() && !localClose && code.getCode() != CloseReason.CloseCodes.TOO_BIG.getCode()) {
                        //2.1.5: we must use 1006 if the close was initiated locally
                        //however we only do this for normal closure
                        //if the close was due to another reason such as a message being too long we need to report the real reason
                        code = CloseReason.CloseCodes.CLOSED_ABNORMALLY;
                    }
                    endpoint.getInstance().onClose(this, new CloseReason(code, reason));
                } catch (Exception e) {
                    endpoint.getInstance().onError(this, e);
                }
            }
        } finally {
            close0();
            if (clientConnectionBuilder != null && !localClose) {
                WebSocketReconnectHandler webSocketReconnectHandler = container.getWebSocketReconnectHandler();
                if (webSocketReconnectHandler != null) {
                    JsrWebSocketLogger.REQUEST_LOGGER.debugf("Calling reconnect handler for %s", this);
                    long reconnect = webSocketReconnectHandler.disconnected(closeReason, requestUri, this, ++disconnectCount);
                    if (reconnect >= 0) {
                        handleReconnect(reconnect);
                    }
                }
            }
        }
    }
}
Also used : CloseReason(javax.websocket.CloseReason) CloseMessage(io.undertow.websockets.core.CloseMessage) IOException(java.io.IOException)

Example 2 with CloseMessage

use of io.undertow.websockets.core.CloseMessage in project spring-framework by spring-projects.

the class UndertowWebSocketHandlerAdapter method onFullCloseMessage.

@Override
@SuppressWarnings("deprecation")
protected void onFullCloseMessage(WebSocketChannel channel, BufferedBinaryMessage message) {
    CloseMessage closeMessage = new CloseMessage(message.getData().getResource());
    this.session.handleClose(CloseStatus.create(closeMessage.getCode(), closeMessage.getReason()));
    message.getData().free();
}
Also used : CloseMessage(io.undertow.websockets.core.CloseMessage)

Aggregations

CloseMessage (io.undertow.websockets.core.CloseMessage)2 IOException (java.io.IOException)1 CloseReason (javax.websocket.CloseReason)1