io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker13 Java Examples

The following examples show how to use io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker13. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebsocketConnectionBuilder.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
public <R> CompletableFuture<R> connect(Function<Channel, R> connectFunction) {
    io.netty.bootstrap.Bootstrap b = new io.netty.bootstrap.Bootstrap();
    int actualPort = uri.getPort() == -1 ? (uri.getScheme().equals("wss") ? 443 : 80) : uri.getPort();

    final WebSocketClientHandler handler =
            new WebSocketClientHandler(
                    new WebSocketClientHandshaker13(
                            uri, WebSocketVersion.V13, null, false, HttpHeaders.EMPTY_HEADERS, 1280000) {

                        @Override
                        protected FullHttpRequest newHandshakeRequest() {
                            FullHttpRequest request = super.newHandshakeRequest();
                            if (clientNegotiation.getSupportedSubProtocols() != null) {
                                StringBuilder sb = new StringBuilder();
                                for (int i = 0; i < clientNegotiation.getSupportedSubProtocols().size(); ++i) {
                                    if (i > 0) {
                                        sb.append(", ");
                                    }
                                    sb.append(clientNegotiation.getSupportedSubProtocols().get(i));
                                }
                                request.headers().add(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, sb.toString());
                            }
                            clientNegotiation.beforeRequest(request.headers());
                            return request;
                        }

                        @Override
                        protected void verify(FullHttpResponse response) {
                            super.verify(response);
                            clientNegotiation.afterRequest(response.headers());
                        }
                    }, connectFunction);

    b.group(eventLoopGroup)
            .channel(NioSocketChannel.class)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();
                    if (ssl != null) {
                        SSLEngine sslEngine = ssl.createSSLEngine(uri.getHost(), actualPort);
                        sslEngine.setUseClientMode(true);
                        pipeline.addLast("ssl", new SslHandler(sslEngine));
                    }
                    pipeline.addLast("http-codec", new HttpClientCodec());
                    pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
                    pipeline.addLast("ws-handler", handler);
                }
            });

    //System.out.println("WebSocket Client connecting");
    b.connect(uri.getHost(), actualPort).addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.cause() != null) {
                handler.handshakeFuture.completeExceptionally(future.cause());
            }
        }
    });


    return handler.handshakeFuture;
}