Search in sources :

Example 1 with StandardWebSocketSession

use of org.springframework.web.socket.adapter.standard.StandardWebSocketSession in project spring-framework by spring-projects.

the class AbstractStandardUpgradeStrategy method upgrade.

@Override
public void upgrade(ServerHttpRequest request, ServerHttpResponse response, String selectedProtocol, List<WebSocketExtension> selectedExtensions, Principal user, WebSocketHandler wsHandler, Map<String, Object> attrs) throws HandshakeFailureException {
    HttpHeaders headers = request.getHeaders();
    InetSocketAddress localAddr = null;
    try {
        localAddr = request.getLocalAddress();
    } catch (Exception ex) {
    // Ignore
    }
    InetSocketAddress remoteAddr = null;
    try {
        remoteAddr = request.getRemoteAddress();
    } catch (Exception ex) {
    // Ignore
    }
    StandardWebSocketSession session = new StandardWebSocketSession(headers, attrs, localAddr, remoteAddr, user);
    StandardWebSocketHandlerAdapter endpoint = new StandardWebSocketHandlerAdapter(wsHandler, session);
    List<Extension> extensions = new ArrayList<>();
    for (WebSocketExtension extension : selectedExtensions) {
        extensions.add(new WebSocketToStandardExtensionAdapter(extension));
    }
    upgradeInternal(request, response, selectedProtocol, extensions, endpoint);
}
Also used : Extension(javax.websocket.Extension) WebSocketExtension(org.springframework.web.socket.WebSocketExtension) HttpHeaders(org.springframework.http.HttpHeaders) WebSocketExtension(org.springframework.web.socket.WebSocketExtension) StandardWebSocketHandlerAdapter(org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter) StandardWebSocketSession(org.springframework.web.socket.adapter.standard.StandardWebSocketSession) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) WebSocketToStandardExtensionAdapter(org.springframework.web.socket.adapter.standard.WebSocketToStandardExtensionAdapter)

Example 2 with StandardWebSocketSession

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

Aggregations

InetSocketAddress (java.net.InetSocketAddress)2 StandardWebSocketHandlerAdapter (org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter)2 StandardWebSocketSession (org.springframework.web.socket.adapter.standard.StandardWebSocketSession)2 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)1 Endpoint (javax.websocket.Endpoint)1 Extension (javax.websocket.Extension)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ListenableFutureTask (org.springframework.util.concurrent.ListenableFutureTask)1 WebSocketExtension (org.springframework.web.socket.WebSocketExtension)1 WebSocketSession (org.springframework.web.socket.WebSocketSession)1 WebSocketToStandardExtensionAdapter (org.springframework.web.socket.adapter.standard.WebSocketToStandardExtensionAdapter)1 HandshakeFailureException (org.springframework.web.socket.server.HandshakeFailureException)1