Search in sources :

Example 1 with SessionLimitExceededException

use of org.springframework.web.socket.handler.SessionLimitExceededException in project spring-framework by spring-projects.

the class StompSubProtocolHandler method sendToClient.

private void sendToClient(WebSocketSession session, StompHeaderAccessor stompAccessor, byte[] payload) {
    StompCommand command = stompAccessor.getCommand();
    try {
        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
            }
        }
    }
}
Also used : SockJsSession(org.springframework.web.socket.sockjs.transport.SockJsSession) SessionLimitExceededException(org.springframework.web.socket.handler.SessionLimitExceededException) IOException(java.io.IOException) StompCommand(org.springframework.messaging.simp.stomp.StompCommand) BinaryMessage(org.springframework.web.socket.BinaryMessage) TextMessage(org.springframework.web.socket.TextMessage)

Example 2 with SessionLimitExceededException

use of org.springframework.web.socket.handler.SessionLimitExceededException in project spring-framework by spring-projects.

the class SubProtocolWebSocketHandler method handleMessage.

/**
	 * Handle an outbound Spring Message to a WebSocket client.
	 */
@Override
public void handleMessage(Message<?> message) throws MessagingException {
    String sessionId = resolveSessionId(message);
    if (sessionId == null) {
        if (logger.isErrorEnabled()) {
            logger.error("Couldn't find session id in " + message);
        }
        return;
    }
    WebSocketSessionHolder holder = this.sessions.get(sessionId);
    if (holder == null) {
        if (logger.isDebugEnabled()) {
            // The broker may not have removed the session yet
            logger.debug("No session for " + message);
        }
        return;
    }
    WebSocketSession session = holder.getSession();
    try {
        findProtocolHandler(session).handleMessageToClient(session, message);
    } catch (SessionLimitExceededException ex) {
        try {
            if (logger.isDebugEnabled()) {
                logger.debug("Terminating '" + session + "'", ex);
            }
            this.stats.incrementLimitExceededCount();
            // clear first, session may be unresponsive
            clearSession(session, ex.getStatus());
            session.close(ex.getStatus());
        } catch (Exception secondException) {
            logger.debug("Failure while closing session " + sessionId + ".", secondException);
        }
    } catch (Exception ex) {
        // Could be part of normal workflow (e.g. browser tab closed)
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to send message to client in " + session + ": " + message, ex);
        }
    }
}
Also used : SessionLimitExceededException(org.springframework.web.socket.handler.SessionLimitExceededException) MessagingException(org.springframework.messaging.MessagingException) SessionLimitExceededException(org.springframework.web.socket.handler.SessionLimitExceededException) IOException(java.io.IOException) WebSocketSession(org.springframework.web.socket.WebSocketSession)

Aggregations

IOException (java.io.IOException)2 SessionLimitExceededException (org.springframework.web.socket.handler.SessionLimitExceededException)2 MessagingException (org.springframework.messaging.MessagingException)1 StompCommand (org.springframework.messaging.simp.stomp.StompCommand)1 BinaryMessage (org.springframework.web.socket.BinaryMessage)1 TextMessage (org.springframework.web.socket.TextMessage)1 WebSocketSession (org.springframework.web.socket.WebSocketSession)1 SockJsSession (org.springframework.web.socket.sockjs.transport.SockJsSession)1