Search in sources :

Example 51 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);
    assertEquals(1, this.session.getSentMessages().size());
    TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
    assertEquals("RECEIPT\n" + "receipt-id:message-123\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)

Example 52 with TextMessage

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

the class StompSubProtocolHandlerTests method handleMessageToClientWithSimpConnectAckDefaultHeartBeat.

@Test
public void handleMessageToClientWithSimpConnectAckDefaultHeartBeat() {
    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);
    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:0,0\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)

Example 53 with TextMessage

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

the class WebSocketStompClientTests method handleWebSocketMessage.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void handleWebSocketMessage() throws Exception {
    String text = "SEND\na:alpha\n\nMessage payload\0";
    connect().handleMessage(this.webSocketSession, new TextMessage(text));
    ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
    verify(this.stompSession).handleMessage(captor.capture());
    Message<byte[]> message = captor.getValue();
    assertNotNull(message);
    StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
    StompHeaders headers = StompHeaders.readOnlyStompHeaders(accessor.toNativeHeaderMap());
    assertEquals(StompCommand.SEND, accessor.getCommand());
    assertEquals("alpha", headers.getFirst("a"));
    assertEquals("Message payload", new String(message.getPayload(), StandardCharsets.UTF_8));
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) PongMessage(org.springframework.web.socket.PongMessage) Message(org.springframework.messaging.Message) BinaryMessage(org.springframework.web.socket.BinaryMessage) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) StompHeaders(org.springframework.messaging.simp.stomp.StompHeaders) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 54 with TextMessage

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

the class WebSocketStompClientTests method sendWebSocketMessage.

@Test
public void sendWebSocketMessage() throws Exception {
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND);
    accessor.setDestination("/topic/foo");
    byte[] payload = "payload".getBytes(StandardCharsets.UTF_8);
    getTcpConnection().send(MessageBuilder.createMessage(payload, accessor.getMessageHeaders()));
    ArgumentCaptor<TextMessage> textMessageCaptor = ArgumentCaptor.forClass(TextMessage.class);
    verify(this.webSocketSession).sendMessage(textMessageCaptor.capture());
    TextMessage textMessage = textMessageCaptor.getValue();
    assertNotNull(textMessage);
    assertEquals("SEND\ndestination:/topic/foo\ncontent-length:7\n\npayload\0", textMessage.getPayload());
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 55 with TextMessage

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

the class WebSocketServerSockJsSession method initializeDelegateSession.

public void initializeDelegateSession(WebSocketSession session) {
    synchronized (this.initSessionLock) {
        this.webSocketSession = session;
        try {
            // Let "our" handler know before sending the open frame to the remote handler
            delegateConnectionEstablished();
            this.webSocketSession.sendMessage(new TextMessage(SockJsFrame.openFrame().getContent()));
            // Flush any messages cached in the mean time
            while (!this.initSessionCache.isEmpty()) {
                writeFrame(SockJsFrame.messageFrame(getMessageCodec(), this.initSessionCache.poll()));
            }
            scheduleHeartbeat();
            this.openFrameSent = true;
        } catch (Throwable ex) {
            tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
        }
    }
}
Also used : 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