Search in sources :

Example 81 with TextMessage

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

the class ConcurrentWebSocketSessionDecoratorTests method send.

@Test
public void send() throws IOException {
    TestWebSocketSession session = new TestWebSocketSession();
    session.setOpen(true);
    ConcurrentWebSocketSessionDecorator decorator = new ConcurrentWebSocketSessionDecorator(session, 1000, 1024);
    TextMessage textMessage = new TextMessage("payload");
    decorator.sendMessage(textMessage);
    assertThat(session.getSentMessages().size()).isEqualTo(1);
    assertThat(session.getSentMessages().get(0)).isEqualTo(textMessage);
    assertThat(decorator.getBufferSize()).isEqualTo(0);
    assertThat(decorator.getTimeSinceSendStarted()).isEqualTo(0);
    assertThat(session.isOpen()).isTrue();
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Example 82 with TextMessage

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

the class ClientSockJsSessionTests method handleFrameMessageWithWebSocketHandlerException.

@Test
public void handleFrameMessageWithWebSocketHandlerException() throws Exception {
    this.session.handleFrame(SockJsFrame.openFrame().getContent());
    willThrow(new IllegalStateException("Fake error")).given(this.handler).handleMessage(this.session, new TextMessage("foo"));
    willThrow(new IllegalStateException("Fake error")).given(this.handler).handleMessage(this.session, new TextMessage("bar"));
    this.session.handleFrame(SockJsFrame.messageFrame(CODEC, "foo", "bar").getContent());
    assertThat(this.session.isOpen()).isTrue();
    verify(this.handler).afterConnectionEstablished(this.session);
    verify(this.handler).handleMessage(this.session, new TextMessage("foo"));
    verify(this.handler).handleMessage(this.session, new TextMessage("bar"));
    verifyNoMoreInteractions(this.handler);
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Example 83 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("https://example.com");
    transport.executeSendRequest(url, requestHeaders, new TextMessage("payload"));
    assertThat(transport.actualSendRequestHeaders.size()).isEqualTo(2);
    assertThat(transport.actualSendRequestHeaders.getFirst("foo")).isEqualTo("bar");
    assertThat(transport.actualSendRequestHeaders.getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) URI(java.net.URI) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Example 84 with TextMessage

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

the class AbstractSockJsIntegrationTests method testEcho.

private void testEcho(int messageCount, Transport transport, WebSocketHttpHeaders headers) throws Exception {
    List<TextMessage> messages = new ArrayList<>();
    for (int i = 0; i < messageCount; i++) {
        messages.add(new TextMessage("m" + i));
    }
    TestClientHandler handler = new TestClientHandler();
    initSockJsClient(transport);
    URI url = new URI(this.baseUrl + "/echo");
    WebSocketSession session = this.sockJsClient.doHandshake(handler, headers, url).get();
    for (TextMessage message : messages) {
        session.sendMessage(message);
    }
    handler.awaitMessageCount(messageCount, 5000);
    for (TextMessage message : messages) {
        assertThat(handler.receivedMessages.remove(message)).as("Message not received: " + message).isTrue();
    }
    assertThat(handler.receivedMessages.size()).as("Remaining messages: " + handler.receivedMessages).isEqualTo(0);
    session.close();
}
Also used : ArrayList(java.util.ArrayList) URI(java.net.URI) TextMessage(org.springframework.web.socket.TextMessage) WebSocketSession(org.springframework.web.socket.WebSocketSession)

Example 85 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);
    assertThat(clientHandler.session).as("afterConnectionEstablished should have been called").isNotNull();
    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)92 Test (org.junit.jupiter.api.Test)42 WebSocketSession (org.springframework.web.socket.WebSocketSession)20 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)16 IOException (java.io.IOException)14 Test (org.junit.Test)8 BinaryMessage (org.springframework.web.socket.BinaryMessage)8 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)7 URI (java.net.URI)6 Message (org.springframework.messaging.Message)6 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)6 StompEncoder (org.springframework.messaging.simp.stomp.StompEncoder)4 CloseStatus (org.springframework.web.socket.CloseStatus)4 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)4 ArrayList (java.util.ArrayList)3 MessageChannel (org.springframework.messaging.MessageChannel)3 ExecutorSubscribableChannel (org.springframework.messaging.support.ExecutorSubscribableChannel)3 ImmutableMessageChannelInterceptor (org.springframework.messaging.support.ImmutableMessageChannelInterceptor)3 ShellLogResponseMessage (com.ibeiliao.deployment.admin.websocket.request.ShellLogResponseMessage)2 UncheckedIOException (java.io.UncheckedIOException)2