Search in sources :

Example 56 with TextMessage

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

the class StompSubProtocolHandler method sendErrorMessage.

/**
	 * Invoked when no
	 * {@link #setErrorHandler(StompSubProtocolErrorHandler) errorHandler}
	 * is configured to send an ERROR frame to the client.
	 */
private void sendErrorMessage(WebSocketSession session, Throwable error) {
    StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.ERROR);
    headerAccessor.setMessage(error.getMessage());
    byte[] bytes = this.stompEncoder.encode(headerAccessor.getMessageHeaders(), EMPTY_PAYLOAD);
    try {
        session.sendMessage(new TextMessage(bytes));
    } catch (Throwable ex) {
        // Could be part of normal workflow (e.g. browser tab closed)
        logger.debug("Failed to send STOMP ERROR to client", ex);
    }
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage)

Example 57 with TextMessage

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

the class RestTemplateXhrTransportTests method connectReceiveAndCloseWithStompFrame.

@Test
public void connectReceiveAndCloseWithStompFrame() throws Exception {
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND);
    accessor.setDestination("/destination");
    MessageHeaders headers = accessor.getMessageHeaders();
    Message<byte[]> message = MessageBuilder.createMessage("body".getBytes(StandardCharsets.UTF_8), headers);
    byte[] bytes = new StompEncoder().encode(message);
    TextMessage textMessage = new TextMessage(bytes);
    SockJsFrame frame = SockJsFrame.messageFrame(new Jackson2SockJsMessageCodec(), textMessage.getPayload());
    String body = "o\n" + frame.getContent() + "\n" + "c[3000,\"Go away!\"]";
    ClientHttpResponse response = response(HttpStatus.OK, body);
    connect(response);
    verify(this.webSocketHandler).afterConnectionEstablished(any());
    verify(this.webSocketHandler).handleMessage(any(), eq(textMessage));
    verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!")));
    verifyNoMoreInteractions(this.webSocketHandler);
}
Also used : Jackson2SockJsMessageCodec(org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec) StompEncoder(org.springframework.messaging.simp.stomp.StompEncoder) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) MessageHeaders(org.springframework.messaging.MessageHeaders) CloseStatus(org.springframework.web.socket.CloseStatus) SockJsFrame(org.springframework.web.socket.sockjs.frame.SockJsFrame) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 58 with TextMessage

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

the class XhrTransportTests method sendMessageError.

@Test(expected = HttpServerErrorException.class)
public void sendMessageError() throws Exception {
    TestXhrTransport transport = new TestXhrTransport();
    transport.sendMessageResponseToReturn = new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    URI url = new URI("http://example.com");
    transport.executeSendRequest(url, null, new TextMessage("payload"));
}
Also used : URI(java.net.URI) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 59 with TextMessage

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

the class XhrTransportTests method sendMessage.

@Test
public void sendMessage() throws Exception {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("foo", "bar");
    requestHeaders.setContentType(MediaType.APPLICATION_JSON);
    TestXhrTransport transport = new TestXhrTransport();
    transport.sendMessageResponseToReturn = new ResponseEntity<>(HttpStatus.NO_CONTENT);
    URI url = new URI("http://example.com");
    transport.executeSendRequest(url, requestHeaders, new TextMessage("payload"));
    assertEquals(2, transport.actualSendRequestHeaders.size());
    assertEquals("bar", transport.actualSendRequestHeaders.getFirst("foo"));
    assertEquals(MediaType.APPLICATION_JSON, transport.actualSendRequestHeaders.getContentType());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) URI(java.net.URI) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 60 with TextMessage

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

the class AbstractSockJsIntegrationTests method testReceiveOneMessage.

private void testReceiveOneMessage(Transport transport, WebSocketHttpHeaders headers) throws Exception {
    TestClientHandler clientHandler = new TestClientHandler();
    initSockJsClient(transport);
    this.sockJsClient.doHandshake(clientHandler, headers, new URI(this.baseUrl + "/test")).get();
    TestServerHandler serverHandler = this.wac.getBean(TestServerHandler.class);
    assertNotNull("afterConnectionEstablished should have been called", clientHandler.session);
    serverHandler.awaitSession(5000);
    TextMessage message = new TextMessage("message1");
    serverHandler.session.sendMessage(message);
    clientHandler.awaitMessage(message, 5000);
}
Also used : URI(java.net.URI) TextMessage(org.springframework.web.socket.TextMessage)

Aggregations

TextMessage (org.springframework.web.socket.TextMessage)67 Test (org.junit.Test)48 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)16 WebSocketSession (org.springframework.web.socket.WebSocketSession)11 IOException (java.io.IOException)8 BinaryMessage (org.springframework.web.socket.BinaryMessage)8 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)7 Message (org.springframework.messaging.Message)6 URI (java.net.URI)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 StompEncoder (org.springframework.messaging.simp.stomp.StompEncoder)4 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)4 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)4 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)3 MessageChannel (org.springframework.messaging.MessageChannel)3 ExecutorSubscribableChannel (org.springframework.messaging.support.ExecutorSubscribableChannel)3 ImmutableMessageChannelInterceptor (org.springframework.messaging.support.ImmutableMessageChannelInterceptor)3 CloseStatus (org.springframework.web.socket.CloseStatus)3 TestWebSocketSession (org.springframework.web.socket.handler.TestWebSocketSession)3 SockJsMessageDeliveryException (org.springframework.web.socket.sockjs.SockJsMessageDeliveryException)3