Search in sources :

Example 6 with TextMessage

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

the class AbstractClientSockJsSession method sendMessage.

@Override
public final void sendMessage(WebSocketMessage<?> message) throws IOException {
    if (!(message instanceof TextMessage)) {
        throw new IllegalArgumentException(this + " supports text messages only.");
    }
    if (this.state != State.OPEN) {
        throw new IllegalStateException(this + " is not open: current state " + this.state);
    }
    String payload = ((TextMessage) message).getPayload();
    payload = getMessageCodec().encode(payload);
    // the client-side doesn't need message framing (letter "a")
    payload = payload.substring(1);
    TextMessage messageToSend = new TextMessage(payload);
    if (logger.isTraceEnabled()) {
        logger.trace("Sending message " + messageToSend + " in " + this);
    }
    sendInternal(messageToSend);
}
Also used : TextMessage(org.springframework.web.socket.TextMessage)

Example 7 with TextMessage

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

the class WebSocketMessageBrokerConfigurationSupportTests method clientInboundChannelSendMessage.

@Test
public void clientInboundChannelSendMessage() throws Exception {
    ApplicationContext config = createConfig(TestChannelConfig.class, TestConfigurer.class);
    TestChannel channel = config.getBean("clientInboundChannel", TestChannel.class);
    SubProtocolWebSocketHandler webSocketHandler = config.getBean(SubProtocolWebSocketHandler.class);
    List<ChannelInterceptor> interceptors = channel.getInterceptors();
    assertEquals(ImmutableMessageChannelInterceptor.class, interceptors.get(interceptors.size() - 1).getClass());
    TestWebSocketSession session = new TestWebSocketSession("s1");
    session.setOpen(true);
    webSocketHandler.afterConnectionEstablished(session);
    TextMessage textMessage = StompTextMessageBuilder.create(StompCommand.SEND).headers("destination:/foo").build();
    webSocketHandler.handleMessage(session, textMessage);
    Message<?> message = channel.messages.get(0);
    StompHeaderAccessor accessor = StompHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
    assertNotNull(accessor);
    assertFalse(accessor.isMutable());
    assertEquals(SimpMessageType.MESSAGE, accessor.getMessageType());
    assertEquals("/foo", accessor.getDestination());
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) TestWebSocketSession(org.springframework.web.socket.handler.TestWebSocketSession) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 8 with TextMessage

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

the class StompSubProtocolHandlerTests method handleMessageToClientWithBinaryWebSocketMessage.

// SPR-12475
@Test
public void handleMessageToClientWithBinaryWebSocketMessage() {
    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.MESSAGE);
    headers.setMessageId("mess0");
    headers.setSubscriptionId("sub0");
    headers.setContentType(MimeTypeUtils.APPLICATION_OCTET_STREAM);
    headers.setDestination("/queue/foo");
    // Non-empty payload
    byte[] payload = new byte[1];
    Message<byte[]> message = MessageBuilder.createMessage(payload, headers.getMessageHeaders());
    this.protocolHandler.handleMessageToClient(this.session, message);
    assertEquals(1, this.session.getSentMessages().size());
    WebSocketMessage<?> webSocketMessage = this.session.getSentMessages().get(0);
    assertTrue(webSocketMessage instanceof BinaryMessage);
    // Empty payload
    payload = EMPTY_PAYLOAD;
    message = MessageBuilder.createMessage(payload, headers.getMessageHeaders());
    this.protocolHandler.handleMessageToClient(this.session, message);
    assertEquals(2, this.session.getSentMessages().size());
    webSocketMessage = this.session.getSentMessages().get(1);
    assertTrue(webSocketMessage instanceof TextMessage);
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) BinaryMessage(org.springframework.web.socket.BinaryMessage) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 9 with TextMessage

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

the class StompSubProtocolHandlerTests method handleMessageFromClientWithInvalidStompCommand.

@Test
public void handleMessageFromClientWithInvalidStompCommand() {
    TextMessage textMessage = new TextMessage("FOO\n\n\0");
    this.protocolHandler.afterSessionStarted(this.session, this.channel);
    this.protocolHandler.handleMessageFromClient(this.session, textMessage, this.channel);
    verifyZeroInteractions(this.channel);
    assertEquals(1, this.session.getSentMessages().size());
    TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
    assertTrue(actual.getPayload().startsWith("ERROR"));
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 10 with TextMessage

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

the class StompSubProtocolHandlerTests method handleMessageToClientWithSimpConnectAck.

@Test
public void handleMessageToClientWithSimpConnectAck() {
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECT);
    accessor.setHeartbeat(10000, 10000);
    accessor.setAcceptVersion("1.0,1.1");
    Message<?> connectMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
    SimpMessageHeaderAccessor ackAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK);
    ackAccessor.setHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER, connectMessage);
    ackAccessor.setHeader(SimpMessageHeaderAccessor.HEART_BEAT_HEADER, new long[] { 15000, 15000 });
    Message<byte[]> ackMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, ackAccessor.getMessageHeaders());
    this.protocolHandler.handleMessageToClient(this.session, ackMessage);
    assertEquals(1, this.session.getSentMessages().size());
    TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
    assertEquals("CONNECTED\n" + "version:1.1\n" + "heart-beat:15000,15000\n" + "user-name:joe\n" + "\n" + "", actual.getPayload());
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Aggregations

TextMessage (org.springframework.web.socket.TextMessage)65 Test (org.junit.Test)48 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)16 WebSocketSession (org.springframework.web.socket.WebSocketSession)10 BinaryMessage (org.springframework.web.socket.BinaryMessage)8 IOException (java.io.IOException)7 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