Java Code Examples for javax.websocket.CloseReason.CloseCodes#CLOSED_ABNORMALLY

The following examples show how to use javax.websocket.CloseReason.CloseCodes#CLOSED_ABNORMALLY . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WsSession.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void sendCloseMessage(CloseReason closeReason) {
    // 125 is maximum size for the payload of a control message
    ByteBuffer msg = ByteBuffer.allocate(125);
    CloseCode closeCode = closeReason.getCloseCode();
    // CLOSED_ABNORMALLY should not be put on the wire
    if (closeCode == CloseCodes.CLOSED_ABNORMALLY) {
        // PROTOCOL_ERROR is probably better than GOING_AWAY here
        msg.putShort((short) CloseCodes.PROTOCOL_ERROR.getCode());
    } else {
        msg.putShort((short) closeCode.getCode());
    }

    String reason = closeReason.getReasonPhrase();
    if (reason != null && reason.length() > 0) {
        appendCloseReasonWithTruncation(msg, reason);
    }
    msg.flip();
    try {
        wsRemoteEndpoint.sendMessageBlock(Constants.OPCODE_CLOSE, msg, true);
    } catch (IOException | WritePendingException e) {
        // Failed to send close message. Close the socket and let the caller
        // deal with the Exception
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("wsSession.sendCloseFail", id), e);
        }
        wsRemoteEndpoint.close();
        // Failure to send a close message is not unexpected in the case of
        // an abnormal closure (usually triggered by a failure to read/write
        // from/to the client. In this case do not trigger the endpoint's
        // error handling
        if (closeCode != CloseCodes.CLOSED_ABNORMALLY) {
            localEndpoint.onError(this, e);
        }
    } finally {
        webSocketContainer.unregisterSession(getSessionMapKey(), this);
    }
}
 
Example 2
Source File: Snake.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
protected void sendMessage(String msg) {
    try {
        session.getBasicRemote().sendText(msg);
    } catch (IOException ioe) {
        CloseReason cr =
                new CloseReason(CloseCodes.CLOSED_ABNORMALLY, ioe.getMessage());
        try {
            session.close(cr);
        } catch (IOException ioe2) {
            // Ignore
        }
    }
}
 
Example 3
Source File: WsFrameBase.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void handleThrowableOnSend(Throwable t) throws WsIOException {
    ExceptionUtils.handleThrowable(t);
    wsSession.getLocal().onError(wsSession, t);
    CloseReason cr = new CloseReason(CloseCodes.CLOSED_ABNORMALLY,
            sm.getString("wsFrame.ioeTriggeredClose"));
    throw new WsIOException(cr);
}
 
Example 4
Source File: Snake.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
protected void sendMessage(String msg) {
    try {
        session.getBasicRemote().sendText(msg);
    } catch (IOException ioe) {
        CloseReason cr =
                new CloseReason(CloseCodes.CLOSED_ABNORMALLY, ioe.getMessage());
        try {
            session.close(cr);
        } catch (IOException ioe2) {
            // Ignore
        }
    }
}
 
Example 5
Source File: WsSession.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private void sendCloseMessage(CloseReason closeReason) {
    // 125 is maximum size for the payload of a control message
    ByteBuffer msg = ByteBuffer.allocate(125);
    CloseCode closeCode = closeReason.getCloseCode();
    // CLOSED_ABNORMALLY should not be put on the wire
    if (closeCode == CloseCodes.CLOSED_ABNORMALLY) {
        // PROTOCOL_ERROR is probably better than GOING_AWAY here
        msg.putShort((short) CloseCodes.PROTOCOL_ERROR.getCode());
    } else {
        msg.putShort((short) closeCode.getCode());
    }

    String reason = closeReason.getReasonPhrase();
    if (reason != null && reason.length() > 0) {
        appendCloseReasonWithTruncation(msg, reason);
    }
    msg.flip();
    try {
        wsRemoteEndpoint.startMessageBlock(
                Constants.OPCODE_CLOSE, msg, true);
    } catch (IOException ioe) {
        // Failed to send close message. Close the socket and let the caller
        // deal with the Exception
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("wsSession.sendCloseFail", id), ioe);
        }
        wsRemoteEndpoint.close();
        // Failure to send a close message is not unexpected in the case of
        // an abnormal closure (usually triggered by a failure to read/write
        // from/to the client. In this case do not trigger the endpoint's
        // error handling
        if (closeCode != CloseCodes.CLOSED_ABNORMALLY) {
            localEndpoint.onError(this, ioe);
        }
    } finally {
        webSocketContainer.unregisterSession(localEndpoint, this);
    }
}
 
Example 6
Source File: WsFrameBase.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private void handleThrowableOnSend(Throwable t) throws WsIOException {
    ExceptionUtils.handleThrowable(t);
    wsSession.getLocal().onError(wsSession, t);
    CloseReason cr = new CloseReason(CloseCodes.CLOSED_ABNORMALLY,
            sm.getString("wsFrame.ioeTriggeredClose"));
    throw new WsIOException(cr);
}
 
Example 7
Source File: Snake.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
protected void sendMessage(String msg) {
    try {
        session.getBasicRemote().sendText(msg);
    } catch (IOException ioe) {
        CloseReason cr =
                new CloseReason(CloseCodes.CLOSED_ABNORMALLY, ioe.getMessage());
        try {
            session.close(cr);
        } catch (IOException ioe2) {
            // Ignore
        }
    }
}
 
Example 8
Source File: WsSession.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private void sendCloseMessage(CloseReason closeReason) {
    // 125 is maximum size for the payload of a control message
    ByteBuffer msg = ByteBuffer.allocate(125);
    CloseCode closeCode = closeReason.getCloseCode();
    // CLOSED_ABNORMALLY should not be put on the wire
    if (closeCode == CloseCodes.CLOSED_ABNORMALLY) {
        // PROTOCOL_ERROR is probably better than GOING_AWAY here
        msg.putShort((short) CloseCodes.PROTOCOL_ERROR.getCode());
    } else {
        msg.putShort((short) closeCode.getCode());
    }

    String reason = closeReason.getReasonPhrase();
    if (reason != null && reason.length() > 0) {
        appendCloseReasonWithTruncation(msg, reason);
    }
    msg.flip();
    try {
        wsRemoteEndpoint.startMessageBlock(Constants.OPCODE_CLOSE, msg, true);
    } catch (IOException ioe) {
        handleCloseException(ioe, closeCode);
    } catch (WritePendingException wpe) {
        handleCloseException(wpe, closeCode);
    } finally {
        webSocketContainer.unregisterSession(localEndpoint, this);
    }
}
 
Example 9
Source File: WsSession.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private void handleCloseException(Exception e, CloseCode closeCode) {
    // Failed to send close message. Close the socket and let the caller
    // deal with the Exception
    if (log.isDebugEnabled()) {
        log.debug(sm.getString("wsSession.sendCloseFail", id), e);
    }
    wsRemoteEndpoint.close();
    // Failure to send a close message is not unexpected in the case of
    // an abnormal closure (usually triggered by a failure to read/write
    // from/to the client. In this case do not trigger the endpoint's
    // error handling
    if (closeCode != CloseCodes.CLOSED_ABNORMALLY) {
        localEndpoint.onError(this, e);
    }
}
 
Example 10
Source File: WsFrameBase.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private void handleThrowableOnSend(Throwable t) throws WsIOException {
    ExceptionUtils.handleThrowable(t);
    wsSession.getLocal().onError(wsSession, t);
    CloseReason cr = new CloseReason(CloseCodes.CLOSED_ABNORMALLY,
            sm.getString("wsFrame.ioeTriggeredClose"));
    throw new WsIOException(cr);
}
 
Example 11
Source File: WsHttpUpgradeHandler.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public void onDataAvailable() {
    try {
        wsFrame.onDataAvailable();
    } catch (WsIOException ws) {
        wsProtocolHandler.close(ws.getCloseReason());
    } catch (IOException ioe) {
        onError(ioe);
        CloseReason cr = new CloseReason(
                CloseCodes.CLOSED_ABNORMALLY, ioe.getMessage());
        wsProtocolHandler.close(cr);
    }
}
 
Example 12
Source File: Snake.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
protected void sendMessage(String msg) {
    try {
        session.getBasicRemote().sendText(msg);
    } catch (IOException ioe) {
        CloseReason cr =
                new CloseReason(CloseCodes.CLOSED_ABNORMALLY, ioe.getMessage());
        try {
            session.close(cr);
        } catch (IOException ioe2) {
            // Ignore
        }
    }
}
 
Example 13
Source File: Snake.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
protected void sendMessage(String msg) {
    try {
        session.getBasicRemote().sendText(msg);
    } catch (IOException ioe) {
        CloseReason cr =
                new CloseReason(CloseCodes.CLOSED_ABNORMALLY, ioe.getMessage());
        try {
            session.close(cr);
        } catch (IOException ioe2) {
            // Ignore
        }
    }
}