Search in sources :

Example 71 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.STOMP).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();
    assertThat(actual).isNotNull();
    assertThat(SimpMessageHeaderAccessor.getSessionId(actual.getHeaders())).isEqualTo("s1");
    assertThat(SimpMessageHeaderAccessor.getSessionAttributes(actual.getHeaders())).isNotNull();
    assertThat(SimpMessageHeaderAccessor.getUser(actual.getHeaders())).isNotNull();
    assertThat(SimpMessageHeaderAccessor.getUser(actual.getHeaders()).getName()).isEqualTo("joe");
    assertThat(SimpMessageHeaderAccessor.getHeartbeat(actual.getHeaders())).isNotNull();
    assertThat(SimpMessageHeaderAccessor.getHeartbeat(actual.getHeaders())).isEqualTo(new long[] { 10000, 10000 });
    StompHeaderAccessor stompAccessor = StompHeaderAccessor.wrap(actual);
    assertThat(stompAccessor.getCommand()).isEqualTo(StompCommand.STOMP);
    assertThat(stompAccessor.getLogin()).isEqualTo("guest");
    assertThat(stompAccessor.getPasscode()).isEqualTo("guest");
    assertThat(stompAccessor.getHeartbeat()).isEqualTo(new long[] { 10000, 10000 });
    assertThat(stompAccessor.getAcceptVersion()).isEqualTo(new HashSet<>(Arrays.asList("1.1", "1.0")));
    assertThat(this.session.getSentMessages().size()).isEqualTo(0);
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Example 72 with TextMessage

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

the class StompSubProtocolHandlerTests method handleMessageToClientWithSimpDisconnectAckAndReceipt.

@Test
public void handleMessageToClientWithSimpDisconnectAckAndReceipt() {
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.DISCONNECT);
    accessor.setReceipt("message-123");
    Message<?> connectMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
    SimpMessageHeaderAccessor ackAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT_ACK);
    ackAccessor.setHeader(SimpMessageHeaderAccessor.DISCONNECT_MESSAGE_HEADER, connectMessage);
    Message<byte[]> ackMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, ackAccessor.getMessageHeaders());
    this.protocolHandler.handleMessageToClient(this.session, ackMessage);
    assertThat(this.session.getSentMessages().size()).isEqualTo(1);
    TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
    assertThat(actual.getPayload()).isEqualTo(("RECEIPT\n" + "receipt-id:message-123\n" + "\n\u0000"));
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.jupiter.api.Test)

Example 73 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 ChannelInterceptor() {

        @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);
    assertThat(mutable.get()).isNotNull();
    assertThat(mutable.get()).isTrue();
}
Also used : ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) 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) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) 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.jupiter.api.Test)

Example 74 with TextMessage

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

the class StompSubProtocolHandlerTests method handleMessageFromClientWithoutImmutableMessageInterceptor.

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

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

Example 75 with TextMessage

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

the class StompWebSocketIntegrationTests method webSocketScope.

@ParameterizedWebSocketTest
void webSocketScope(WebSocketTestServer server, WebSocketClient webSocketClient, TestInfo testInfo) throws Exception {
    super.setup(server, webSocketClient, testInfo);
    TextMessage m0 = create(StompCommand.CONNECT).headers("accept-version:1.1").build();
    TextMessage m1 = create(StompCommand.SUBSCRIBE).headers("id:subs1", "destination:/topic/scopedBeanValue").build();
    TextMessage m2 = create(StompCommand.SEND).headers("destination:/app/scopedBeanValue").build();
    TestClientWebSocketHandler clientHandler = new TestClientWebSocketHandler(2, m0, m1, m2);
    try (WebSocketSession session = doHandshake(clientHandler, "/ws").get()) {
        assertThat(session).isNotNull();
        assertThat(clientHandler.latch.await(TIMEOUT, TimeUnit.SECONDS)).isTrue();
        String payload = clientHandler.actual.get(1).getPayload();
        assertThat(payload.startsWith("MESSAGE\n")).isTrue();
        assertThat(payload.contains("destination:/topic/scopedBeanValue\n")).isTrue();
        assertThat(payload.endsWith("55\0")).isTrue();
    }
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) WebSocketSession(org.springframework.web.socket.WebSocketSession)

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