Search in sources :

Example 1 with SockJsSession

use of org.springframework.web.socket.sockjs.transport.SockJsSession 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 SockJsSession

use of org.springframework.web.socket.sockjs.transport.SockJsSession in project spring-framework by spring-projects.

the class StompSubProtocolHandler method afterStompSessionConnected.

private StompHeaderAccessor afterStompSessionConnected(Message<?> message, StompHeaderAccessor accessor, WebSocketSession session) {
    Principal principal = getUser(session);
    if (principal != null) {
        accessor = toMutableAccessor(accessor, message);
        accessor.setNativeHeader(CONNECTED_USER_HEADER, principal.getName());
    }
    long[] heartbeat = accessor.getHeartbeat();
    if (heartbeat[1] > 0) {
        session = WebSocketSessionDecorator.unwrap(session);
        if (session instanceof SockJsSession) {
            ((SockJsSession) session).disableHeartbeat();
        }
    }
    return accessor;
}
Also used : SockJsSession(org.springframework.web.socket.sockjs.transport.SockJsSession) Principal(java.security.Principal)

Example 3 with SockJsSession

use of org.springframework.web.socket.sockjs.transport.SockJsSession in project spring-framework by spring-projects.

the class StompSubProtocolHandlerTests method handleMessageToClientWithHeartbeatSuppressingSockJsHeartbeat.

@Test
public void handleMessageToClientWithHeartbeatSuppressingSockJsHeartbeat() throws IOException {
    SockJsSession sockJsSession = Mockito.mock(SockJsSession.class);
    when(sockJsSession.getId()).thenReturn("s1");
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECTED);
    accessor.setHeartbeat(0, 10);
    Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
    this.protocolHandler.handleMessageToClient(sockJsSession, message);
    verify(sockJsSession).getId();
    verify(sockJsSession).getPrincipal();
    verify(sockJsSession).disableHeartbeat();
    verify(sockJsSession).sendMessage(any(WebSocketMessage.class));
    verifyNoMoreInteractions(sockJsSession);
    sockJsSession = Mockito.mock(SockJsSession.class);
    when(sockJsSession.getId()).thenReturn("s1");
    accessor = StompHeaderAccessor.create(StompCommand.CONNECTED);
    accessor.setHeartbeat(0, 0);
    message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
    this.protocolHandler.handleMessageToClient(sockJsSession, message);
    verify(sockJsSession).getId();
    verify(sockJsSession).getPrincipal();
    verify(sockJsSession).sendMessage(any(WebSocketMessage.class));
    verifyNoMoreInteractions(sockJsSession);
}
Also used : SockJsSession(org.springframework.web.socket.sockjs.transport.SockJsSession) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) WebSocketMessage(org.springframework.web.socket.WebSocketMessage) Test(org.junit.Test)

Aggregations

SockJsSession (org.springframework.web.socket.sockjs.transport.SockJsSession)3 IOException (java.io.IOException)1 Principal (java.security.Principal)1 Test (org.junit.Test)1 StompCommand (org.springframework.messaging.simp.stomp.StompCommand)1 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)1 BinaryMessage (org.springframework.web.socket.BinaryMessage)1 TextMessage (org.springframework.web.socket.TextMessage)1 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)1 SessionLimitExceededException (org.springframework.web.socket.handler.SessionLimitExceededException)1