Netty WebScoketClientHandshaker无法正常工作

时间:2016-11-03 02:31:06

标签: websocket netty

我有一个问题。谁能帮我?它会非常感激。我想做websocketclient但是我的代码在handShaker.handshake(ch).syncUninterruptibly(); ... websocket服务器运行良好,因为我使用JS.only netty客户端无法正常测试它。

下面是我的代码

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();
    }
}

0 个答案:

没有答案