Search in sources :

Example 1 with WebSocketClientExtensionHandler

use of io.netty.handler.codec.http.websocketx.extensions.WebSocketClientExtensionHandler in project vert.x by eclipse.

the class Http1xClientConnection method toWebSocket.

synchronized void toWebSocket(ContextInternal context, String requestURI, MultiMap headers, boolean allowOriginHeader, WebsocketVersion vers, List<String> subProtocols, int maxWebSocketFrameSize, Promise<WebSocket> promise) {
    try {
        URI wsuri = new URI(requestURI);
        if (!wsuri.isAbsolute()) {
            // Netty requires an absolute url
            wsuri = new URI((ssl ? "https:" : "http:") + "//" + server.host() + ":" + server.port() + requestURI);
        }
        WebSocketVersion version = WebSocketVersion.valueOf((vers == null ? WebSocketVersion.V13 : vers).toString());
        HttpHeaders nettyHeaders;
        if (headers != null) {
            nettyHeaders = new DefaultHttpHeaders();
            for (Map.Entry<String, String> entry : headers) {
                nettyHeaders.add(entry.getKey(), entry.getValue());
            }
        } else {
            nettyHeaders = null;
        }
        ChannelPipeline p = chctx.channel().pipeline();
        ArrayList<WebSocketClientExtensionHandshaker> extensionHandshakers = initializeWebSocketExtensionHandshakers(client.getOptions());
        if (!extensionHandshakers.isEmpty()) {
            p.addBefore("handler", "webSocketsExtensionsHandler", new WebSocketClientExtensionHandler(extensionHandshakers.toArray(new WebSocketClientExtensionHandshaker[0])));
        }
        String subp = null;
        if (subProtocols != null) {
            subp = String.join(",", subProtocols);
        }
        WebSocketClientHandshaker handshaker = newHandshaker(wsuri, version, subp, !extensionHandshakers.isEmpty(), allowOriginHeader, nettyHeaders, maxWebSocketFrameSize, !options.isSendUnmaskedFrames());
        WebSocketHandshakeInboundHandler handshakeInboundHandler = new WebSocketHandshakeInboundHandler(handshaker, ar -> {
            AsyncResult<WebSocket> wsRes = ar.map(v -> {
                WebSocketImpl w = new WebSocketImpl(context, Http1xClientConnection.this, version != WebSocketVersion.V00, options.getWebSocketClosingTimeout(), options.getMaxWebSocketFrameSize(), options.getMaxWebSocketMessageSize());
                w.subProtocol(handshaker.actualSubprotocol());
                return w;
            });
            if (ar.failed()) {
                close();
            } else {
                webSocket = (WebSocketImpl) wsRes.result();
                webSocket.registerHandler(vertx.eventBus());
                log.debug("WebSocket handshake complete");
                HttpClientMetrics metrics = client.metrics();
                if (metrics != null) {
                    webSocket.setMetric(metrics.connected(webSocket));
                }
            }
            getContext().emit(wsRes, res -> {
                if (res.succeeded()) {
                    webSocket.headers(ar.result());
                }
                promise.handle(res);
                if (res.succeeded()) {
                    webSocket.headers(null);
                }
            });
        });
        p.addBefore("handler", "handshakeCompleter", handshakeInboundHandler);
        handshaker.handshake(chctx.channel());
    } catch (Exception e) {
        handleException(e);
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) HttpHeaders(io.vertx.core.http.HttpHeaders) WebSocketClientExtensionHandler(io.netty.handler.codec.http.websocketx.extensions.WebSocketClientExtensionHandler) HttpClientMetrics(io.vertx.core.spi.metrics.HttpClientMetrics) URI(java.net.URI) WebSocketClientExtensionHandshaker(io.netty.handler.codec.http.websocketx.extensions.WebSocketClientExtensionHandshaker)

Aggregations

HttpHeaders (io.netty.handler.codec.http.HttpHeaders)1 WebSocketClientExtensionHandler (io.netty.handler.codec.http.websocketx.extensions.WebSocketClientExtensionHandler)1 WebSocketClientExtensionHandshaker (io.netty.handler.codec.http.websocketx.extensions.WebSocketClientExtensionHandshaker)1 HttpHeaders (io.vertx.core.http.HttpHeaders)1 HttpClientMetrics (io.vertx.core.spi.metrics.HttpClientMetrics)1 URI (java.net.URI)1