Search in sources :

Example 16 with TextMessage

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

the class AbstractSockJsSession method delegateMessages.

public void delegateMessages(String... messages) throws SockJsMessageDeliveryException {
    List<String> undelivered = new ArrayList<>(Arrays.asList(messages));
    for (String message : messages) {
        try {
            if (isClosed()) {
                throw new SockJsMessageDeliveryException(this.id, undelivered, "Session closed");
            } else {
                this.handler.handleMessage(this, new TextMessage(message));
                undelivered.remove(0);
            }
        } catch (Throwable ex) {
            throw new SockJsMessageDeliveryException(this.id, undelivered, ex);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) SockJsMessageDeliveryException(org.springframework.web.socket.sockjs.SockJsMessageDeliveryException) TextMessage(org.springframework.web.socket.TextMessage)

Example 17 with TextMessage

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

the class WebSocketServerSockJsSession method writeFrameInternal.

@Override
protected void writeFrameInternal(SockJsFrame frame) throws IOException {
    if (logger.isTraceEnabled()) {
        logger.trace("Writing " + frame);
    }
    TextMessage message = new TextMessage(frame.getContent());
    this.webSocketSession.sendMessage(message);
}
Also used : TextMessage(org.springframework.web.socket.TextMessage)

Example 18 with TextMessage

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

the class StompWebSocketIntegrationTests method sendSubscribeToControllerAndReceiveReply.

// SPR-11648
@Test
public void sendSubscribeToControllerAndReceiveReply() throws Exception {
    String destHeader = "destination:/app/number";
    TextMessage message = create(StompCommand.SUBSCRIBE).headers("id:subs1", destHeader).build();
    TestClientWebSocketHandler clientHandler = new TestClientWebSocketHandler(1, message);
    WebSocketSession session = doHandshake(clientHandler, "/ws").get();
    try {
        assertTrue(clientHandler.latch.await(TIMEOUT, TimeUnit.SECONDS));
        String payload = clientHandler.actual.get(0).getPayload();
        assertTrue("Expected STOMP destination=/app/number, got " + payload, payload.contains(destHeader));
        assertTrue("Expected STOMP Payload=42, got " + payload, payload.contains("42"));
    } finally {
        session.close();
    }
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) WebSocketSession(org.springframework.web.socket.WebSocketSession) Test(org.junit.Test)

Example 19 with TextMessage

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

the class StompWebSocketIntegrationTests method sendMessageToController.

@Test
public void sendMessageToController() throws Exception {
    TextMessage message = create(StompCommand.SEND).headers("destination:/app/simple").build();
    WebSocketSession session = doHandshake(new TestClientWebSocketHandler(0, message), "/ws").get();
    SimpleController controller = this.wac.getBean(SimpleController.class);
    try {
        assertTrue(controller.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)

Example 20 with TextMessage

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

the class StompWebSocketIntegrationTests method webSocketScope.

@Test
public void webSocketScope() throws Exception {
    TextMessage message1 = create(StompCommand.SUBSCRIBE).headers("id:subs1", "destination:/topic/scopedBeanValue").build();
    TextMessage message2 = create(StompCommand.SEND).headers("destination:/app/scopedBeanValue").build();
    TestClientWebSocketHandler clientHandler = new TestClientWebSocketHandler(1, message1, message2);
    WebSocketSession session = doHandshake(clientHandler, "/ws").get();
    try {
        assertTrue(clientHandler.latch.await(TIMEOUT, TimeUnit.SECONDS));
        String payload = clientHandler.actual.get(0).getPayload();
        assertTrue(payload.startsWith("MESSAGE\n"));
        assertTrue(payload.contains("destination:/topic/scopedBeanValue\n"));
        assertTrue(payload.endsWith("55\0"));
    } 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)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