netty同时绑定80和443端口

public static void main(String[] args) throws Exception {
        ContextProvider.onStart();
        File keyFile = new File("/out/my.pk8"); //使用pkcs8格式的私钥
        File crtFile = new File("/out/my.crt");
        EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            /** 使用已有的证书 */
            final SslContext ctx = SslContextBuilder.forServer(crtFile /** 此处为证书, 不是公钥 */,
                    keyFile /** 私钥*/, "123456" /** 私钥密码, 如果没有密码则不填 */).build();
             /** 让netty随机生成证书 */
            //SelfSignedCertificate cert = new SelfSignedCertificate();
            //final SslContext ctx = SslContext.newServerContext(cert.certificate(), cert.privateKey());                    
            ServerBootstrap b = new ServerBootstrap(); // (2)
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class) // (3)
                    .childHandler(new ChannelInitializer<SocketChannel>() { // (4)
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipe = ch.pipeline();
                            if (ch.localAddress().getPort() == 443) {
                            	/** 如果是443端口, 则需要添加ssl处理器 */
                                pipe.addLast(ctx.newHandler(ch.alloc()));
                            }
                            pipe.addLast(new RtspDecoder()).addLast(new RTSPHandler());
                            pipe.addLast(new ReadTimeoutHandler(30));
                        }
                    })
                    .option(ChannelOption.SO_BACKLOG, 128)          // (5)
                    .childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
            List<ChannelFuture> futures = new ArrayList<>();
            futures.add(b.bind(80));
            futures.add(b.bind(443));
            for (ChannelFuture f : futures) {
                f.channel().closeFuture().sync();
            }
        } catch (Exception ex) {
            logger.error("start netty failed, ", ex);
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值