Netty webSocket client 怎么连接wss地址

 
 
private Bootstrap connectionBootstrap = new Bootstrap();
private static final EventLoopGroup group = new NioEventLoopGroup();
private WebSocketClientNettyAdapter clientHandler = null;
private Channel socketChannel = null;
private final Timer _connectTimer = new Timer();


public WebSocketClientNetty(URI uri, IWebSocketController controller) {
    super(uri, controller);

    final WebSocketClientNetty t = this;

    connectionBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);

    final int idleTime = DigiNet.SETTINGS.webSocketClientConfig.disconnectIdleTime / 1000;

    connectionBootstrap.group(group)
            .channel(NioSocketChannel.class)
            //.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(100*1024*1024))
            .handler(new ChannelInitializer<Channel>() {
                @Override
                protected void initChannel(final Channel ch) throws Exception {
                    clientHandler = new WebSocketClientNettyAdapter(WebSocketClientHandshakerFactory.newHandshaker(_uri, WebSocketVersion.V13, null, true, HttpHeaders.EMPTY_HEADERS, 100 * 1024 * 1024), t);
                    ChannelPipeline pipeline = ch.pipeline();
                    SSLEngine sslEngine = SSLContext.getDefault().createSSLEngine();
                    sslEngine.setUseClientMode(true);
                    pipeline.addLast("ssl", new SslHandler(sslEngine));
                    pipeline.addLast(
                      new HttpClientCodec(),
                      new HttpObjectAggregator(8192),
                      new IdleStateHandler(idleTime, idleTime, idleTime),
                      DgtWebSocketClientCompressionHandler.INSTANCE,
                      clientHandler
                    );
                }
            });
}

只要在initChannel的pipline中加入SslHandler即可。


猜你喜欢

转载自blog.csdn.net/mafei6827/article/details/80657405
今日推荐