Search in sources :

Example 1 with CloseStatus

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

the class RestTemplateXhrTransport method connectInternal.

@Override
protected void connectInternal(final TransportRequest transportRequest, final WebSocketHandler handler, final URI receiveUrl, final HttpHeaders handshakeHeaders, final XhrClientSockJsSession session, final SettableListenableFuture<WebSocketSession> connectFuture) {
    getTaskExecutor().execute(new Runnable() {

        @Override
        public void run() {
            HttpHeaders httpHeaders = transportRequest.getHttpRequestHeaders();
            XhrRequestCallback requestCallback = new XhrRequestCallback(handshakeHeaders);
            XhrRequestCallback requestCallbackAfterHandshake = new XhrRequestCallback(httpHeaders);
            XhrReceiveExtractor responseExtractor = new XhrReceiveExtractor(session);
            while (true) {
                if (session.isDisconnected()) {
                    session.afterTransportClosed(null);
                    break;
                }
                try {
                    if (logger.isTraceEnabled()) {
                        logger.trace("Starting XHR receive request, url=" + receiveUrl);
                    }
                    getRestTemplate().execute(receiveUrl, HttpMethod.POST, requestCallback, responseExtractor);
                    requestCallback = requestCallbackAfterHandshake;
                } catch (Throwable ex) {
                    if (!connectFuture.isDone()) {
                        connectFuture.setException(ex);
                    } else {
                        session.handleTransportError(ex);
                        session.afterTransportClosed(new CloseStatus(1006, ex.getMessage()));
                    }
                    break;
                }
            }
        }
    });
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) CloseStatus(org.springframework.web.socket.CloseStatus)

Example 2 with CloseStatus

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

the class ExceptionWebSocketHandlerDecoratorTests method afterConnectionClosed.

@Test
public void afterConnectionClosed() throws Exception {
    CloseStatus closeStatus = CloseStatus.NORMAL;
    willThrow(new IllegalStateException("error")).given(this.delegate).afterConnectionClosed(this.session, closeStatus);
    this.decorator.afterConnectionClosed(this.session, closeStatus);
    assertNull(this.session.getCloseStatus());
}
Also used : CloseStatus(org.springframework.web.socket.CloseStatus) Test(org.junit.Test)

Example 3 with CloseStatus

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

the class RestTemplateXhrTransportTests method connectReceiveAndCloseWithPrelude.

@Test
public void connectReceiveAndCloseWithPrelude() throws Exception {
    StringBuilder sb = new StringBuilder(2048);
    for (int i = 0; i < 2048; i++) {
        sb.append('h');
    }
    String body = sb.toString() + "\n" + "o\n" + "a[\"foo\"]\n" + "c[3000,\"Go away!\"]";
    ClientHttpResponse response = response(HttpStatus.OK, body);
    connect(response);
    verify(this.webSocketHandler).afterConnectionEstablished(any());
    verify(this.webSocketHandler).handleMessage(any(), eq(new TextMessage("foo")));
    verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!")));
    verifyNoMoreInteractions(this.webSocketHandler);
}
Also used : CloseStatus(org.springframework.web.socket.CloseStatus) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 4 with CloseStatus

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

the class RestTemplateXhrTransportTests method connectReceiveAndClose.

@Test
public void connectReceiveAndClose() throws Exception {
    String body = "o\n" + "a[\"foo\"]\n" + "c[3000,\"Go away!\"]";
    ClientHttpResponse response = response(HttpStatus.OK, body);
    connect(response);
    verify(this.webSocketHandler).afterConnectionEstablished(any());
    verify(this.webSocketHandler).handleMessage(any(), eq(new TextMessage("foo")));
    verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!")));
    verifyNoMoreInteractions(this.webSocketHandler);
}
Also used : CloseStatus(org.springframework.web.socket.CloseStatus) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 5 with CloseStatus

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

the class SockJsSessionTests method close.

@Test
public void close() throws Exception {
    this.session.delegateConnectionEstablished();
    assertOpen();
    this.session.setActive(true);
    this.session.close();
    assertEquals(1, this.session.getSockJsFramesWritten().size());
    assertEquals(SockJsFrame.closeFrameGoAway(), this.session.getSockJsFramesWritten().get(0));
    assertEquals(1, this.session.getNumberOfLastActiveTimeUpdates());
    assertTrue(this.session.didCancelHeartbeat());
    assertEquals(new CloseStatus(3000, "Go away!"), this.session.getCloseStatus());
    assertClosed();
    verify(this.webSocketHandler).afterConnectionClosed(this.session, new CloseStatus(3000, "Go away!"));
}
Also used : CloseStatus(org.springframework.web.socket.CloseStatus) Test(org.junit.Test)

Aggregations

CloseStatus (org.springframework.web.socket.CloseStatus)13 Test (org.junit.Test)9 IOException (java.io.IOException)3 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)3 TextMessage (org.springframework.web.socket.TextMessage)3 ClientCallback (io.undertow.client.ClientCallback)1 ClientExchange (io.undertow.client.ClientExchange)1 ClientResponse (io.undertow.client.ClientResponse)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpStatus (org.springframework.http.HttpStatus)1 MessageHeaders (org.springframework.messaging.MessageHeaders)1 StompEncoder (org.springframework.messaging.simp.stomp.StompEncoder)1 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)1 SimpUser (org.springframework.messaging.simp.user.SimpUser)1 HttpServerErrorException (org.springframework.web.client.HttpServerErrorException)1 Jackson2SockJsMessageCodec (org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec)1 SockJsFrame (org.springframework.web.socket.sockjs.frame.SockJsFrame)1 StreamSinkChannel (org.xnio.channels.StreamSinkChannel)1