Search in sources :

Example 1 with SockJsMessageDeliveryException

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

the class AbstractSockJsSession method delegateMessages.

public void delegateMessages(String... messages) throws SockJsMessageDeliveryException {
    List<String> undelivered = new ArrayList<>(Arrays.asList(messages));
    for (String message : messages) {
        try {
            if (isClosed()) {
                throw new SockJsMessageDeliveryException(this.id, undelivered, "Session closed");
            } else {
                this.handler.handleMessage(this, new TextMessage(message));
                undelivered.remove(0);
            }
        } catch (Throwable ex) {
            throw new SockJsMessageDeliveryException(this.id, undelivered, ex);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) SockJsMessageDeliveryException(org.springframework.web.socket.sockjs.SockJsMessageDeliveryException) TextMessage(org.springframework.web.socket.TextMessage)

Example 2 with SockJsMessageDeliveryException

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

the class SockJsSessionTests method delegateMessagesWithErrorAndConnectionClosing.

@Test
public void delegateMessagesWithErrorAndConnectionClosing() throws Exception {
    WebSocketHandler wsHandler = new ExceptionWebSocketHandlerDecorator(this.webSocketHandler);
    TestSockJsSession sockJsSession = new TestSockJsSession("1", this.sockJsConfig, wsHandler, Collections.<String, Object>emptyMap());
    String msg1 = "message 1";
    String msg2 = "message 2";
    String msg3 = "message 3";
    willThrow(new IOException()).given(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg2));
    sockJsSession.delegateConnectionEstablished();
    try {
        sockJsSession.delegateMessages(msg1, msg2, msg3);
        fail("expected exception");
    } catch (SockJsMessageDeliveryException ex) {
        assertEquals(Collections.singletonList(msg3), ex.getUndeliveredMessages());
        verify(this.webSocketHandler).afterConnectionEstablished(sockJsSession);
        verify(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg1));
        verify(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg2));
        verify(this.webSocketHandler).afterConnectionClosed(sockJsSession, CloseStatus.SERVER_ERROR);
        verifyNoMoreInteractions(this.webSocketHandler);
    }
}
Also used : ExceptionWebSocketHandlerDecorator(org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) SockJsMessageDeliveryException(org.springframework.web.socket.sockjs.SockJsMessageDeliveryException) IOException(java.io.IOException) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Aggregations

TextMessage (org.springframework.web.socket.TextMessage)2 SockJsMessageDeliveryException (org.springframework.web.socket.sockjs.SockJsMessageDeliveryException)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)1 ExceptionWebSocketHandlerDecorator (org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator)1