下面是我的代码
public void Start() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try{
Bootstrap b = new Bootstrap();
URI uri = new URI(url);
WebSocketClientHandshaker handShaker = WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, null, false, null);
WebSocketClientHandler handler = new WebSocketClientHandler(handShaker, actionHandlers);
b.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new HttpClientCodec());
pipeline.addLast(new HttpObjectAggregator(8192));
pipeline.addLast(handler);
}
});
ch = b.connect(uri.getHost(), uri.getPort()).sync().channel();
//handler.handShakeFuture().sync();
handShaker.handshake(ch).syncUninterruptibly();
ch.closeFuture().awaitUninterruptibly();
}catch(Exception e) {
e.printStackTrace();
}
}