Search in sources :

Example 21 with WebSocketSession

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

the class StandardWebSocketClient method doHandshakeInternal.

@Override
protected ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler webSocketHandler, HttpHeaders headers, final URI uri, List<String> protocols, List<WebSocketExtension> extensions, Map<String, Object> attributes) {
    int port = getPort(uri);
    InetSocketAddress localAddress = new InetSocketAddress(getLocalHost(), port);
    InetSocketAddress remoteAddress = new InetSocketAddress(uri.getHost(), port);
    final StandardWebSocketSession session = new StandardWebSocketSession(headers, attributes, localAddress, remoteAddress);
    final ClientEndpointConfig endpointConfig = ClientEndpointConfig.Builder.create().configurator(new StandardWebSocketClientConfigurator(headers)).preferredSubprotocols(protocols).extensions(adaptExtensions(extensions)).build();
    endpointConfig.getUserProperties().putAll(getUserProperties());
    final Endpoint endpoint = new StandardWebSocketHandlerAdapter(webSocketHandler, session);
    Callable<WebSocketSession> connectTask = new Callable<WebSocketSession>() {

        @Override
        public WebSocketSession call() throws Exception {
            webSocketContainer.connectToServer(endpoint, endpointConfig, uri);
            return session;
        }
    };
    if (this.taskExecutor != null) {
        return this.taskExecutor.submitListenable(connectTask);
    } else {
        ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<>(connectTask);
        task.run();
        return task;
    }
}
Also used : StandardWebSocketHandlerAdapter(org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter) Endpoint(javax.websocket.Endpoint) ListenableFutureTask(org.springframework.util.concurrent.ListenableFutureTask) StandardWebSocketSession(org.springframework.web.socket.adapter.standard.StandardWebSocketSession) InetSocketAddress(java.net.InetSocketAddress) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Endpoint(javax.websocket.Endpoint) Callable(java.util.concurrent.Callable) WebSocketSession(org.springframework.web.socket.WebSocketSession) StandardWebSocketSession(org.springframework.web.socket.adapter.standard.StandardWebSocketSession)

Example 22 with WebSocketSession

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

the class AbstractSockJsIntegrationTests method testEcho.

private void testEcho(int messageCount, Transport transport, WebSocketHttpHeaders headers) throws Exception {
    List<TextMessage> messages = new ArrayList<>();
    for (int i = 0; i < messageCount; i++) {
        messages.add(new TextMessage("m" + i));
    }
    TestClientHandler handler = new TestClientHandler();
    initSockJsClient(transport);
    URI url = new URI(this.baseUrl + "/echo");
    WebSocketSession session = this.sockJsClient.doHandshake(handler, headers, url).get();
    for (TextMessage message : messages) {
        session.sendMessage(message);
    }
    handler.awaitMessageCount(messageCount, 5000);
    for (TextMessage message : messages) {
        assertTrue("Message not received: " + message, handler.receivedMessages.remove(message));
    }
    assertEquals("Remaining messages: " + handler.receivedMessages, 0, handler.receivedMessages.size());
    session.close();
}
Also used : ArrayList(java.util.ArrayList) URI(java.net.URI) TextMessage(org.springframework.web.socket.TextMessage) WebSocketSession(org.springframework.web.socket.WebSocketSession)

Example 23 with WebSocketSession

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

the class JettyWebSocketClient method doHandshakeInternal.

@Override
public ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler wsHandler, HttpHeaders headers, final URI uri, List<String> protocols, List<WebSocketExtension> extensions, Map<String, Object> attributes) {
    final ClientUpgradeRequest request = new ClientUpgradeRequest();
    request.setSubProtocols(protocols);
    for (WebSocketExtension e : extensions) {
        request.addExtensions(new WebSocketToJettyExtensionConfigAdapter(e));
    }
    for (String header : headers.keySet()) {
        request.setHeader(header, headers.get(header));
    }
    Principal user = getUser();
    final JettyWebSocketSession wsSession = new JettyWebSocketSession(attributes, user);
    final JettyWebSocketHandlerAdapter listener = new JettyWebSocketHandlerAdapter(wsHandler, wsSession);
    Callable<WebSocketSession> connectTask = new Callable<WebSocketSession>() {

        @Override
        public WebSocketSession call() throws Exception {
            Future<Session> future = client.connect(listener, uri, request);
            future.get();
            return wsSession;
        }
    };
    if (this.taskExecutor != null) {
        return this.taskExecutor.submitListenable(connectTask);
    } else {
        ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<>(connectTask);
        task.run();
        return task;
    }
}
Also used : WebSocketExtension(org.springframework.web.socket.WebSocketExtension) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession) JettyWebSocketHandlerAdapter(org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter) Callable(java.util.concurrent.Callable) WebSocketSession(org.springframework.web.socket.WebSocketSession) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession) ListenableFutureTask(org.springframework.util.concurrent.ListenableFutureTask) WebSocketToJettyExtensionConfigAdapter(org.springframework.web.socket.adapter.jetty.WebSocketToJettyExtensionConfigAdapter) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) Principal(java.security.Principal) WebSocketSession(org.springframework.web.socket.WebSocketSession) Session(org.eclipse.jetty.websocket.api.Session) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession)

Example 24 with WebSocketSession

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

the class StandardWebSocketClientTests method testGetLocalAddress.

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

Example 25 with WebSocketSession

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

the class StandardWebSocketClientTests method taskExecutor.

@Test
public void taskExecutor() throws Exception {
    URI uri = new URI("ws://localhost/abc");
    this.wsClient.setTaskExecutor(new SimpleAsyncTaskExecutor());
    WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
    assertNotNull(session);
}
Also used : SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) 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