use of io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker in project undertow by undertow-io.
the class WebSocketTestClient method connect.
/**
* Connect the WebSocket client
*
* @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;
}
Aggregations