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

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

Introduction

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

Prototype

public ChannelFuture handshake(Channel channel) 

Source Link

Document

Begins the opening handshake

Usage

From source file:io.undertow.websockets.utils.WebSocketTestClient.java

License:Open Source License

/**
 * Connect the WebSocket client// ww w  .  j a v a2s .  c  om
 *
 * @throws Exception
 */
public WebSocketTestClient connect() throws Exception {
    String protocol = uri.getScheme();
    if (!"ws".equals(protocol)) {
        throw new IllegalArgumentException("Unsupported protocol: " + protocol);
    }
    final WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri, version,
            null, false, new DefaultHttpHeaders());

    EventLoopGroup group = new NioEventLoopGroup();
    final CountDownLatch handshakeLatch = new CountDownLatch(1);
    bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer() {
        @Override
        protected void initChannel(Channel channel) throws Exception {

            ChannelPipeline p = channel.pipeline();
            p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192),
                    new WSClientHandler(handshaker, handshakeLatch));
        }
    });

    // Connect
    ChannelFuture future = bootstrap.connect(new InetSocketAddress(uri.getHost(), uri.getPort()));
    future.syncUninterruptibly();

    ch = future.channel();

    handshaker.handshake(ch).syncUninterruptibly();
    handshakeLatch.await();

    return this;
}

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

License:Open Source License

/**
 * @param host host of the WebSocket client.
 * @param port port of the WebSocket client.
 * @return true if the handshake is done properly.
 * @throws URISyntaxException throws if there is an error in the URI syntax.
 * @throws InterruptedException throws if the connecting the server is interrupted.
 *//*  ww w  .j a  va  2s .  co m*/
public boolean handhshake(String host, int port) throws InterruptedException, URISyntaxException {
    URI uri = new URI(url);
    EventLoopGroup group = new NioEventLoopGroup();
    Bootstrap b = new Bootstrap();
    b.group(group).channel(NioSocketChannel.class).handler(new WebSocketClientInitializer());
    channel = b.connect(host, port).sync().channel();
    WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri,
            WebSocketVersion.V13, null, true, new DefaultHttpHeaders());
    boolean isDone = handshaker.handshake(channel).sync().isSuccess();
    return isDone;
}