Search in sources :

Example 6 with StandardWebSocketClient

use of org.springframework.web.socket.client.standard.StandardWebSocketClient in project spring-framework by spring-projects.

the class WebSocketStompClientIntegrationTests method setUp.

@Before
public void setUp() throws Exception {
    logger.debug("Setting up before '" + this.testName.getMethodName() + "'");
    this.wac = new AnnotationConfigWebApplicationContext();
    this.wac.register(TestConfig.class);
    this.wac.refresh();
    this.server = new TomcatWebSocketTestServer();
    this.server.setup();
    this.server.deployConfig(this.wac);
    this.server.start();
    WebSocketClient webSocketClient = new StandardWebSocketClient();
    this.stompClient = new WebSocketStompClient(webSocketClient);
    this.stompClient.setMessageConverter(new StringMessageConverter());
}
Also used : StringMessageConverter(org.springframework.messaging.converter.StringMessageConverter) TomcatWebSocketTestServer(org.springframework.web.socket.TomcatWebSocketTestServer) StandardWebSocketClient(org.springframework.web.socket.client.standard.StandardWebSocketClient) WebSocketClient(org.springframework.web.socket.client.WebSocketClient) StandardWebSocketClient(org.springframework.web.socket.client.standard.StandardWebSocketClient) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) Before(org.junit.Before)

Example 7 with StandardWebSocketClient

use of org.springframework.web.socket.client.standard.StandardWebSocketClient in project spring-boot by spring-projects.

the class LiveReloadServerTests method connect.

private LiveReloadWebSocketHandler connect() throws Exception {
    WebSocketClient client = new StandardWebSocketClient(new WsWebSocketContainer());
    LiveReloadWebSocketHandler handler = new LiveReloadWebSocketHandler();
    client.doHandshake(handler, "ws://localhost:" + this.port + "/livereload");
    handler.awaitHello();
    return handler;
}
Also used : WsWebSocketContainer(org.apache.tomcat.websocket.WsWebSocketContainer) StandardWebSocketClient(org.springframework.web.socket.client.standard.StandardWebSocketClient) WebSocketClient(org.springframework.web.socket.client.WebSocketClient) StandardWebSocketClient(org.springframework.web.socket.client.standard.StandardWebSocketClient)

Example 8 with StandardWebSocketClient

use of org.springframework.web.socket.client.standard.StandardWebSocketClient in project spring-session by spring-projects.

the class ApplicationTests method run.

@Test
public void run() throws Exception {
    List<Transport> transports = new ArrayList<>(2);
    transports.add(new WebSocketTransport(new StandardWebSocketClient()));
    transports.add(new RestTemplateXhrTransport());
    SockJsClient sockJsClient = new SockJsClient(transports);
    ListenableFuture<WebSocketSession> wsSession = sockJsClient.doHandshake(this.webSocketHandler, "ws://localhost:" + this.port + "/sockjs");
    this.thrown.expect(ExecutionException.class);
    wsSession.get().sendMessage(new TextMessage("a"));
}
Also used : RestTemplateXhrTransport(org.springframework.web.socket.sockjs.client.RestTemplateXhrTransport) SockJsClient(org.springframework.web.socket.sockjs.client.SockJsClient) ArrayList(java.util.ArrayList) StandardWebSocketClient(org.springframework.web.socket.client.standard.StandardWebSocketClient) RestTemplateXhrTransport(org.springframework.web.socket.sockjs.client.RestTemplateXhrTransport) Transport(org.springframework.web.socket.sockjs.client.Transport) WebSocketTransport(org.springframework.web.socket.sockjs.client.WebSocketTransport) WebSocketTransport(org.springframework.web.socket.sockjs.client.WebSocketTransport) TextMessage(org.springframework.web.socket.TextMessage) WebSocketSession(org.springframework.web.socket.WebSocketSession) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 9 with StandardWebSocketClient

use of org.springframework.web.socket.client.standard.StandardWebSocketClient in project spring-integration by spring-projects.

the class ClientWebSocketContainerTests method testClientWebSocketContainer.

@Test
public void testClientWebSocketContainer() throws Exception {
    final AtomicBoolean failure = new AtomicBoolean();
    StandardWebSocketClient webSocketClient = new StandardWebSocketClient() {

        @Override
        protected ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler webSocketHandler, HttpHeaders headers, URI uri, List<String> protocols, List<WebSocketExtension> extensions, Map<String, Object> attributes) {
            ListenableFuture<WebSocketSession> future = super.doHandshakeInternal(webSocketHandler, headers, uri, protocols, extensions, attributes);
            if (failure.get()) {
                future.cancel(true);
            }
            return future;
        }
    };
    Map<String, Object> userProperties = new HashMap<String, Object>();
    userProperties.put(Constants.IO_TIMEOUT_MS_PROPERTY, "" + (Constants.IO_TIMEOUT_MS_DEFAULT * 6));
    webSocketClient.setUserProperties(userProperties);
    ClientWebSocketContainer container = new ClientWebSocketContainer(webSocketClient, server.getWsBaseUrl() + "/ws/websocket");
    TestWebSocketListener messageListener = new TestWebSocketListener();
    container.setMessageListener(messageListener);
    container.setConnectionTimeout(30);
    container.start();
    WebSocketSession session = container.getSession(null);
    assertNotNull(session);
    assertTrue(session.isOpen());
    assertEquals("v10.stomp", session.getAcceptedProtocol());
    session.sendMessage(new PingMessage());
    assertTrue(messageListener.messageLatch.await(10, TimeUnit.SECONDS));
    container.stop();
    try {
        container.getSession(null);
        fail("IllegalStateException expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(IllegalStateException.class));
        assertEquals(e.getMessage(), "'clientSession' has not been established. Consider to 'start' this container.");
    }
    assertTrue(messageListener.sessionEndedLatch.await(10, TimeUnit.SECONDS));
    assertFalse(session.isOpen());
    assertTrue(messageListener.started);
    assertThat(messageListener.message, instanceOf(PongMessage.class));
    failure.set(true);
    container.start();
    try {
        container.getSession(null);
        fail("IllegalStateException is expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(IllegalStateException.class));
        assertThat(e.getCause(), instanceOf(CancellationException.class));
    }
    failure.set(false);
    container.start();
    session = container.getSession(null);
    assertNotNull(session);
    assertTrue(session.isOpen());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HashMap(java.util.HashMap) StandardWebSocketClient(org.springframework.web.socket.client.standard.StandardWebSocketClient) URI(java.net.URI) PingMessage(org.springframework.web.socket.PingMessage) CancellationException(java.util.concurrent.CancellationException) WebSocketSession(org.springframework.web.socket.WebSocketSession) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) List(java.util.List) PongMessage(org.springframework.web.socket.PongMessage) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 10 with StandardWebSocketClient

use of org.springframework.web.socket.client.standard.StandardWebSocketClient in project tutorials by eugenp.

the class StompClient method main.

public static void main(String[] args) {
    WebSocketClient client = new StandardWebSocketClient();
    WebSocketStompClient stompClient = new WebSocketStompClient(client);
    stompClient.setMessageConverter(new MappingJackson2MessageConverter());
    StompSessionHandler sessionHandler = new MyStompSessionHandler();
    stompClient.connect(URL, sessionHandler);
    // Don't close immediately.
    new Scanner(System.in).nextLine();
}
Also used : Scanner(java.util.Scanner) MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) StompSessionHandler(org.springframework.messaging.simp.stomp.StompSessionHandler) WebSocketStompClient(org.springframework.web.socket.messaging.WebSocketStompClient) StandardWebSocketClient(org.springframework.web.socket.client.standard.StandardWebSocketClient) WebSocketClient(org.springframework.web.socket.client.WebSocketClient) StandardWebSocketClient(org.springframework.web.socket.client.standard.StandardWebSocketClient)

Aggregations

StandardWebSocketClient (org.springframework.web.socket.client.standard.StandardWebSocketClient)12 WebSocketClient (org.springframework.web.socket.client.WebSocketClient)8 WebSocketStompClient (org.springframework.web.socket.messaging.WebSocketStompClient)4 SockJsClient (org.springframework.web.socket.sockjs.client.SockJsClient)4 Transport (org.springframework.web.socket.sockjs.client.Transport)4 WebSocketTransport (org.springframework.web.socket.sockjs.client.WebSocketTransport)4 WsWebSocketContainer (org.apache.tomcat.websocket.WsWebSocketContainer)3 MappingJackson2MessageConverter (org.springframework.messaging.converter.MappingJackson2MessageConverter)3 StringMessageConverter (org.springframework.messaging.converter.StringMessageConverter)3 StompSessionHandler (org.springframework.messaging.simp.stomp.StompSessionHandler)3 RestTemplateXhrTransport (org.springframework.web.socket.sockjs.client.RestTemplateXhrTransport)3 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 Scanner (java.util.Scanner)2 Before (org.junit.Before)2 Test (org.junit.Test)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 RestTemplate (org.springframework.web.client.RestTemplate)2 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)2 TomcatWebSocketTestServer (org.springframework.web.socket.TomcatWebSocketTestServer)2