Search in sources :

Example 21 with ClientEndpointConfig

use of jakarta.websocket.ClientEndpointConfig in project spring-framework by spring-projects.

the class EndpointConnectionManager method openConnection.

@Override
protected void openConnection() {
    this.taskExecutor.execute(() -> {
        try {
            if (logger.isInfoEnabled()) {
                logger.info("Connecting to WebSocket at " + getUri());
            }
            Endpoint endpointToUse = this.endpoint;
            if (endpointToUse == null) {
                Assert.state(this.endpointProvider != null, "No endpoint set");
                endpointToUse = this.endpointProvider.getHandler();
            }
            ClientEndpointConfig endpointConfig = this.configBuilder.build();
            this.session = getWebSocketContainer().connectToServer(endpointToUse, endpointConfig, getUri());
            logger.info("Successfully connected to WebSocket");
        } catch (Throwable ex) {
            logger.error("Failed to connect to WebSocket", ex);
        }
    });
}
Also used : Endpoint(jakarta.websocket.Endpoint) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig)

Example 22 with ClientEndpointConfig

use of jakarta.websocket.ClientEndpointConfig 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 = () -> {
        this.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(jakarta.websocket.Endpoint) ListenableFutureTask(org.springframework.util.concurrent.ListenableFutureTask) StandardWebSocketSession(org.springframework.web.socket.adapter.standard.StandardWebSocketSession) InetSocketAddress(java.net.InetSocketAddress) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) Endpoint(jakarta.websocket.Endpoint) WebSocketSession(org.springframework.web.socket.WebSocketSession) StandardWebSocketSession(org.springframework.web.socket.adapter.standard.StandardWebSocketSession)

Aggregations

ClientEndpointConfig (jakarta.websocket.ClientEndpointConfig)22 URI (java.net.URI)15 Context (org.apache.catalina.Context)13 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)13 Tomcat (org.apache.catalina.startup.Tomcat)13 Test (org.junit.Test)12 Session (jakarta.websocket.Session)11 WebSocketContainer (jakarta.websocket.WebSocketContainer)11 Endpoint (jakarta.websocket.Endpoint)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)7 SSLContext (javax.net.ssl.SSLContext)6 TesterProgrammaticEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint)5 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 KeyStore (java.security.KeyStore)3 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)3 Test (org.junit.jupiter.api.Test)3 ClientEndpoint (jakarta.websocket.ClientEndpoint)2