Search in sources :

Example 41 with TextMessage

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

the class EchoWebSocketHandler method handleTextMessage.

@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
    String echoMessage = this.echoService.getMessage(message.getPayload());
    logger.debug(echoMessage);
    session.sendMessage(new TextMessage(echoMessage));
}
Also used : TextMessage(org.springframework.web.socket.TextMessage)

Example 42 with TextMessage

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

the class StompSubProtocolHandlerTests method handleMessageFromClientWithImmutableMessageInterceptor.

@Test
public void handleMessageFromClientWithImmutableMessageInterceptor() {
    AtomicReference<Boolean> mutable = new AtomicReference<>();
    ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
    channel.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            mutable.set(MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class).isMutable());
            return message;
        }
    });
    channel.addInterceptor(new ImmutableMessageChannelInterceptor());
    StompSubProtocolHandler handler = new StompSubProtocolHandler();
    handler.afterSessionStarted(this.session, channel);
    TextMessage message = StompTextMessageBuilder.create(StompCommand.CONNECT).build();
    handler.handleMessageFromClient(this.session, message, channel);
    assertNotNull(mutable.get());
    assertTrue(mutable.get());
}
Also used : ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) TextMessage(org.springframework.web.socket.TextMessage) Message(org.springframework.messaging.Message) WebSocketMessage(org.springframework.web.socket.WebSocketMessage) BinaryMessage(org.springframework.web.socket.BinaryMessage) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageChannel(org.springframework.messaging.MessageChannel) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 43 with TextMessage

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

the class StompSubProtocolHandlerTests method handleMessageFromClient.

@Test
public void handleMessageFromClient() {
    TextMessage textMessage = StompTextMessageBuilder.create(StompCommand.CONNECT).headers("login:guest", "passcode:guest", "accept-version:1.1,1.0", "heart-beat:10000,10000").build();
    this.protocolHandler.afterSessionStarted(this.session, this.channel);
    this.protocolHandler.handleMessageFromClient(this.session, textMessage, this.channel);
    verify(this.channel).send(this.messageCaptor.capture());
    Message<?> actual = this.messageCaptor.getValue();
    assertNotNull(actual);
    assertEquals("s1", SimpMessageHeaderAccessor.getSessionId(actual.getHeaders()));
    assertNotNull(SimpMessageHeaderAccessor.getSessionAttributes(actual.getHeaders()));
    assertNotNull(SimpMessageHeaderAccessor.getUser(actual.getHeaders()));
    assertEquals("joe", SimpMessageHeaderAccessor.getUser(actual.getHeaders()).getName());
    assertNotNull(SimpMessageHeaderAccessor.getHeartbeat(actual.getHeaders()));
    assertArrayEquals(new long[] { 10000, 10000 }, SimpMessageHeaderAccessor.getHeartbeat(actual.getHeaders()));
    StompHeaderAccessor stompAccessor = StompHeaderAccessor.wrap(actual);
    assertEquals(StompCommand.CONNECT, stompAccessor.getCommand());
    assertEquals("guest", stompAccessor.getLogin());
    assertEquals("guest", stompAccessor.getPasscode());
    assertArrayEquals(new long[] { 10000, 10000 }, stompAccessor.getHeartbeat());
    assertEquals(new HashSet<>(Arrays.asList("1.1", "1.0")), stompAccessor.getAcceptVersion());
    assertEquals(0, this.session.getSentMessages().size());
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 44 with TextMessage

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

the class StompSubProtocolHandlerTests method eventPublicationWithExceptions.

@Test
public void eventPublicationWithExceptions() {
    ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
    this.protocolHandler.setApplicationEventPublisher(publisher);
    this.protocolHandler.afterSessionStarted(this.session, this.channel);
    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECT);
    Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders());
    TextMessage textMessage = new TextMessage(new StompEncoder().encode(message));
    this.protocolHandler.handleMessageFromClient(this.session, textMessage, this.channel);
    verify(this.channel).send(this.messageCaptor.capture());
    Message<?> actual = this.messageCaptor.getValue();
    assertNotNull(actual);
    assertEquals(StompCommand.CONNECT, StompHeaderAccessor.wrap(actual).getCommand());
    reset(this.channel);
    headers = StompHeaderAccessor.create(StompCommand.CONNECTED);
    message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders());
    this.protocolHandler.handleMessageToClient(this.session, message);
    assertEquals(1, this.session.getSentMessages().size());
    textMessage = (TextMessage) this.session.getSentMessages().get(0);
    assertEquals("CONNECTED\n" + "user-name:joe\n" + "\n" + "", textMessage.getPayload());
    this.protocolHandler.afterSessionEnded(this.session, CloseStatus.BAD_DATA, this.channel);
    verify(this.channel).send(this.messageCaptor.capture());
    actual = this.messageCaptor.getValue();
    assertNotNull(actual);
    StompHeaderAccessor accessor = StompHeaderAccessor.wrap(actual);
    assertEquals(StompCommand.DISCONNECT, accessor.getCommand());
    assertEquals("s1", accessor.getSessionId());
    assertEquals("joe", accessor.getUser().getName());
}
Also used : ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) StompEncoder(org.springframework.messaging.simp.stomp.StompEncoder) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 45 with TextMessage

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

the class StompWebSocketIntegrationTests method sendMessageToControllerAndReceiveReplyViaTopic.

@Test
public void sendMessageToControllerAndReceiveReplyViaTopic() throws Exception {
    TextMessage message1 = create(StompCommand.SUBSCRIBE).headers("id:subs1", "destination:/topic/increment").build();
    TextMessage message2 = create(StompCommand.SEND).headers("destination:/app/increment").body("5").build();
    TestClientWebSocketHandler clientHandler = new TestClientWebSocketHandler(1, message1, message2);
    WebSocketSession session = doHandshake(clientHandler, "/ws").get();
    try {
        assertTrue(clientHandler.latch.await(TIMEOUT, TimeUnit.SECONDS));
    } finally {
        session.close();
    }
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) WebSocketSession(org.springframework.web.socket.WebSocketSession) Test(org.junit.Test)

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