Search in sources :

Example 6 with WebSocketSession

use of org.springframework.web.socket.WebSocketSession 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 7 with WebSocketSession

use of org.springframework.web.socket.WebSocketSession 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 8 with WebSocketSession

use of org.springframework.web.socket.WebSocketSession 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)

Example 9 with WebSocketSession

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

the class StompWebSocketIntegrationTests method sendMessageToBrokerAndReceiveReplyViaTopic.

// SPR-10930
@Test
public void sendMessageToBrokerAndReceiveReplyViaTopic() throws Exception {
    TextMessage m1 = create(StompCommand.SUBSCRIBE).headers("id:subs1", "destination:/topic/foo").build();
    TextMessage m2 = create(StompCommand.SEND).headers("destination:/topic/foo").body("5").build();
    TestClientWebSocketHandler clientHandler = new TestClientWebSocketHandler(1, m1, m2);
    WebSocketSession session = doHandshake(clientHandler, "/ws").get();
    try {
        assertTrue(clientHandler.latch.await(TIMEOUT, TimeUnit.SECONDS));
        String payload = clientHandler.actual.get(0).getPayload();
        assertTrue("Expected STOMP MESSAGE, got " + payload, payload.startsWith("MESSAGE\n"));
    } finally {
        session.close();
    }
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) WebSocketSession(org.springframework.web.socket.WebSocketSession) Test(org.junit.Test)

Example 10 with WebSocketSession

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

the class StandardWebSocketClientTests method testGetLocalAddressWss.

@Test
public void testGetLocalAddressWss() throws Exception {
    URI uri = new URI("wss://localhost/abc");
    WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
    assertNotNull(session.getLocalAddress());
    assertEquals(443, session.getLocalAddress().getPort());
}
Also used : URI(java.net.URI) WebSocketSession(org.springframework.web.socket.WebSocketSession) Test(org.junit.Test)

Aggregations

WebSocketSession (org.springframework.web.socket.WebSocketSession)32 Test (org.junit.Test)24 TextMessage (org.springframework.web.socket.TextMessage)10 URI (java.net.URI)8 TestWebSocketSession (org.springframework.web.socket.handler.TestWebSocketSession)3 Callable (java.util.concurrent.Callable)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)2 ListenableFutureTask (org.springframework.util.concurrent.ListenableFutureTask)2 SettableListenableFuture (org.springframework.util.concurrent.SettableListenableFuture)2 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)2 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)2 SubProtocolWebSocketHandler (org.springframework.web.socket.messaging.SubProtocolWebSocketHandler)2 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 Principal (java.security.Principal)1 ArrayList (java.util.ArrayList)1