Example usage for io.netty.handler.codec.http.websocketx WebSocketClientHandshaker actualSubprotocol

List of usage examples for io.netty.handler.codec.http.websocketx WebSocketClientHandshaker actualSubprotocol

Introduction

In this page you can find the example usage for io.netty.handler.codec.http.websocketx WebSocketClientHandshaker actualSubprotocol.

Prototype

String actualSubprotocol

To view the source code for io.netty.handler.codec.http.websocketx WebSocketClientHandshaker actualSubprotocol.

Click Source Link

Usage

From source file:org.wso2.carbon.transport.http.netty.sender.websocket.WebSocketClient.java

License:Open Source License

/**
 * Handle the handshake with the server.
 *
 * @return handshake future for connection.
 *//*from   w  ww . j av a 2s  .co m*/
public HandshakeFuture handshake() {
    HandshakeFutureImpl handshakeFuture = new HandshakeFutureImpl();
    try {
        URI uri = new URI(url);
        String scheme = uri.getScheme() == null ? "ws" : uri.getScheme();
        final String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
        final int port;
        if (uri.getPort() == -1) {
            if ("ws".equalsIgnoreCase(scheme)) {
                port = 80;
            } else if ("wss".equalsIgnoreCase(scheme)) {
                port = 443;
            } else {
                port = -1;
            }
        } else {
            port = uri.getPort();
        }

        if (!"ws".equalsIgnoreCase(scheme) && !"wss".equalsIgnoreCase(scheme)) {
            log.error("Only WS(S) is supported.");
            throw new SSLException("");
        }

        final boolean ssl = "wss".equalsIgnoreCase(scheme);
        final SslContext sslCtx;
        if (ssl) {
            sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
        } else {
            sslCtx = null;
        }

        group = new NioEventLoopGroup();
        HttpHeaders httpHeaders = new DefaultHttpHeaders();

        // Adding custom headers to the handshake request.

        if (headers != null) {
            headers.entrySet().forEach(entry -> httpHeaders.add(entry.getKey(), entry.getValue()));
        }

        WebSocketClientHandshaker websocketHandshaker = WebSocketClientHandshakerFactory.newHandshaker(uri,
                WebSocketVersion.V13, subProtocols, true, httpHeaders);
        handler = new WebSocketTargetHandler(websocketHandshaker, ssl, url, target, connectorListener);

        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) {
                ChannelPipeline p = ch.pipeline();
                if (sslCtx != null) {
                    p.addLast(sslCtx.newHandler(ch.alloc(), host, port));
                }
                p.addLast(new HttpClientCodec());
                p.addLast(new HttpObjectAggregator(8192));
                p.addLast(WebSocketClientCompressionHandler.INSTANCE);
                if (idleTimeout > 0) {
                    p.addLast(
                            new IdleStateHandler(idleTimeout, idleTimeout, idleTimeout, TimeUnit.MILLISECONDS));
                }
                p.addLast(handler);
            }
        });

        b.connect(uri.getHost(), port).sync();
        ChannelFuture future = handler.handshakeFuture().addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) {
                Throwable cause = future.cause();
                if (future.isSuccess() && cause == null) {
                    WebSocketSessionImpl session = (WebSocketSessionImpl) handler.getChannelSession();
                    String actualSubProtocol = websocketHandshaker.actualSubprotocol();
                    handler.setActualSubProtocol(actualSubProtocol);
                    session.setNegotiatedSubProtocol(actualSubProtocol);
                    session.setIsOpen(true);
                    handshakeFuture.notifySuccess(session);
                } else {
                    handshakeFuture.notifyError(cause);
                }
            }
        }).sync();
        handshakeFuture.setChannelFuture(future);
    } catch (Throwable t) {
        handshakeFuture.notifyError(t);
    }

    return handshakeFuture;
}