Example usage for org.springframework.web.socket CloseStatus PROTOCOL_ERROR

List of usage examples for org.springframework.web.socket CloseStatus PROTOCOL_ERROR

Introduction

In this page you can find the example usage for org.springframework.web.socket CloseStatus PROTOCOL_ERROR.

Prototype

CloseStatus PROTOCOL_ERROR

To view the source code for org.springframework.web.socket CloseStatus PROTOCOL_ERROR.

Click Source Link

Document

"1002 indicates that an endpoint is terminating the connection due to a protocol error."

Usage

From source file:com.company.project.config.StompDisconnectEvent.java

@Override
public void onApplicationEvent(SessionDisconnectEvent event) {
    StompHeaderAccessor sha = StompHeaderAccessor.wrap(event.getMessage());
    CloseStatus status = event.getCloseStatus();

    if (status.equals(CloseStatus.BAD_DATA)) {
        System.out.println("CloseStatus.BAD_DATA");
    }//from  ww w . j  ava2 s  .c o m
    if (status.equals(CloseStatus.GOING_AWAY)) {
        System.out.println("CloseStatus.GOING_AWAY");
    }
    if (status.equals(CloseStatus.NORMAL)) {
        System.out.println("CloseStatus.NORMAL");
    }
    if (status.equals(CloseStatus.NOT_ACCEPTABLE)) {
        System.out.println("CloseStatus.NOT_ACCEPTABLE");
    }
    if (status.equals(CloseStatus.NO_CLOSE_FRAME)) {
        System.out.println("CloseStatus.NO_CLOSE_FRAME");
    }
    if (status.equals(CloseStatus.NO_STATUS_CODE)) {
        System.out.println("CloseStatus.NO_STATUS_CODE");
    }
    if (status.equals(CloseStatus.POLICY_VIOLATION)) {
        System.out.println("CloseStatus.POLICY_VIOLATION");
    }
    if (status.equals(CloseStatus.PROTOCOL_ERROR)) {
        System.out.println("CloseStatus.PROTOCOL_ERROR");
    }
    if (status.equals(CloseStatus.REQUIRED_EXTENSION)) {
        System.out.println("CloseStatus.REQUIRED_EXTENSION");
    }
    if (status.equals(CloseStatus.SERVER_ERROR)) {
        System.out.println("CloseStatus.SERVER_ERROR");
    }
    if (status.equals(CloseStatus.SERVICE_RESTARTED)) {
        System.out.println("CloseStatus.SERVICE_RESTARTED");
    }
    if (status.equals(CloseStatus.SESSION_NOT_RELIABLE)) {
        System.out.println("CloseStatus.SESSION_NOT_RELIABLE");
    }
    if (status.equals(CloseStatus.TLS_HANDSHAKE_FAILURE)) {
        System.out.println("CloseStatus.TLS_HANDSHAKE_FAILURE");
    }
    if (status.equals(CloseStatus.TOO_BIG_TO_PROCESS)) {
        System.out.println("CloseStatus.TOO_BIG_TO_PROCESS");
    }

    System.out.println("CloseStatus: " + status);

    logger.debug("Disconnect event [sessionId: " + sha.getSessionId() + " ]");
    System.out.println("Disconnect event [sessionId: " + event.getSessionId() + " ]");
}

From source file:ch.rasc.wampspring.config.WampSubProtocolHandler.java

/**
 * Handle WAMP messages going back out to WebSocket clients.
 *///  w w w  .  j a v a  2 s . c  o m
@Override
public void handleMessageToClient(WebSocketSession session, Message<?> message) {
    if (!(message instanceof WampMessage)) {
        logger.error("Expected WampMessage. Ignoring " + message + ".");
        return;
    }

    boolean closeWebSocketSession = false;
    try {
        String json = ((WampMessage) message).toJson(this.jsonFactory);
        session.sendMessage(new TextMessage(json));
    } catch (SessionLimitExceededException ex) {
        // Bad session, just get out
        throw ex;
    } catch (Throwable ex) {
        // Could be part of normal workflow (e.g. browser tab closed)
        logger.debug("Failed to send WebSocket message to client in session " + session.getId() + ".", ex);
        closeWebSocketSession = true;
    } finally {
        if (closeWebSocketSession) {
            try {
                session.close(CloseStatus.PROTOCOL_ERROR);
            } catch (IOException ex) {
                // Ignore
            }
        }
    }
}

From source file:org.springframework.messaging.simp.stomp.StompProtocolHandler.java

/**
 * Handle STOMP messages going back out to WebSocket clients.
 *///from  w  w  w  .  j  a v a  2 s .  com
@Override
public void handleMessageToClient(WebSocketSession session, Message<?> message) {

    StompHeaderAccessor headers = StompHeaderAccessor.wrap(message);
    headers.setCommandIfNotSet(StompCommand.MESSAGE);

    if (StompCommand.CONNECTED.equals(headers.getCommand())) {
        // Ignore for now since we already sent it
        return;
    }

    if (StompCommand.MESSAGE.equals(headers.getCommand()) && (headers.getSubscriptionId() == null)) {
        // TODO: failed message delivery mechanism
        logger.error("Ignoring message, no subscriptionId header: " + message);
        return;
    }

    if (!(message.getPayload() instanceof byte[])) {
        // TODO: failed message delivery mechanism
        logger.error("Ignoring message, expected byte[] content: " + message);
        return;
    }

    try {
        message = MessageBuilder.withPayloadAndHeaders(message.getPayload(), headers).build();
        byte[] bytes = this.stompMessageConverter.fromMessage(message);
        session.sendMessage(new TextMessage(new String(bytes, Charset.forName("UTF-8"))));
    } catch (Throwable t) {
        sendErrorMessage(session, t);
    } finally {
        if (StompCommand.ERROR.equals(headers.getCommand())) {
            try {
                session.close(CloseStatus.PROTOCOL_ERROR);
            } catch (IOException e) {
            }
        }
    }
}

From source file:org.springframework.messaging.simp.stomp.StompWebSocketHandler.java

/**
 * Handle STOMP messages going back out to WebSocket clients.
 *///w ww .ja v  a 2 s.co  m
@Override
public void handleMessage(Message<?> message) {

    StompHeaderAccessor headers = StompHeaderAccessor.wrap(message);
    headers.setCommandIfNotSet(StompCommand.MESSAGE);

    if (StompCommand.CONNECTED.equals(headers.getCommand())) {
        // Ignore for now since we already sent it
        return;
    }

    String sessionId = headers.getSessionId();
    if (sessionId == null) {
        // TODO: failed message delivery mechanism
        logger.error("Ignoring message, no sessionId header: " + message);
        return;
    }

    WebSocketSession session = this.sessions.get(sessionId);
    if (session == null) {
        // TODO: failed message delivery mechanism
        logger.error("Ignoring message, sessionId not found: " + message);
        return;
    }

    if (StompCommand.MESSAGE.equals(headers.getCommand()) && (headers.getSubscriptionId() == null)) {
        // TODO: failed message delivery mechanism
        logger.error("Ignoring message, no subscriptionId header: " + message);
        return;
    }

    if (!(message.getPayload() instanceof byte[])) {
        // TODO: failed message delivery mechanism
        logger.error("Ignoring message, expected byte[] content: " + message);
        return;
    }

    try {
        message = MessageBuilder.withPayloadAndHeaders(message.getPayload(), headers).build();
        byte[] bytes = this.stompMessageConverter.fromMessage(message);
        session.sendMessage(new TextMessage(new String(bytes, Charset.forName("UTF-8"))));
    } catch (Throwable t) {
        sendErrorMessage(session, t);
    } finally {
        if (StompCommand.ERROR.equals(headers.getCommand())) {
            try {
                session.close(CloseStatus.PROTOCOL_ERROR);
            } catch (IOException e) {
            }
        }
    }
}

From source file:org.springframework.web.socket.messaging.StompSubProtocolHandler.java

private void sendToClient(WebSocketSession session, StompHeaderAccessor stompAccessor, byte[] payload) {
    StompCommand command = stompAccessor.getCommand();
    try {// www .ja  v  a  2s .  c om
        byte[] bytes = this.stompEncoder.encode(stompAccessor.getMessageHeaders(), payload);
        boolean useBinary = (payload.length > 0 && !(session instanceof SockJsSession)
                && MimeTypeUtils.APPLICATION_OCTET_STREAM.isCompatibleWith(stompAccessor.getContentType()));
        if (useBinary) {
            session.sendMessage(new BinaryMessage(bytes));
        } else {
            session.sendMessage(new TextMessage(bytes));
        }
    } catch (SessionLimitExceededException ex) {
        // Bad session, just get out
        throw ex;
    } catch (Throwable ex) {
        // Could be part of normal workflow (e.g. browser tab closed)
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to send WebSocket message to client in session " + session.getId(), ex);
        }
        command = StompCommand.ERROR;
    } finally {
        if (StompCommand.ERROR.equals(command)) {
            try {
                session.close(CloseStatus.PROTOCOL_ERROR);
            } catch (IOException ex) {
                // Ignore
            }
        }
    }
}