content: package com.lgp.netty.netty_ch6;
  
  import io.netty.channel.Channel;
  import io.netty.channel.ChannelFuture;
  import io.netty.channel.ChannelInitializer;
  import io.netty.channel.group.ChannelGroup;
  import io.netty.handler.ssl.SslContext;
  import io.netty.handler.ssl.util.SelfSignedCertificate;
  
  import java.net.InetSocketAddress;
  
  /**
   * @AUTHOR lgp
   * @DATE 2019/1/2 17:36
   * @DESCRIPTION 扩展 ChatServer
   **/
  public class SecureChatServer extends ChatServer {
  
      private final SslContext context;
  
      public SecureChatServer(SslContext context) {
          this.context = context;
      }
  
      @Override
      protected ChannelInitializer<Channel> createInitializer(ChannelGroup group) {
          return new SecureChatServerIntializer(group, context);    //2返回先前创建的 SecureChatServerInitializer 来启用加密
      }
  
      public static void main(String[] args) throws Exception {
  
          int port = 22222;
          SelfSignedCertificate cert = new SelfSignedCertificate();
          SslContext context = SslContext.newServerContext(cert.certificate(), cert.privateKey());
          final SecureChatServer endpoint = new SecureChatServer(context);
          ChannelFuture future = endpoint.start(new InetSocketAddress(port));
  
          Runtime.getRuntime().addShutdownHook(new Thread(() -> endpoint.destroy()));
          future.channel().closeFuture().syncUninterruptibly();
      }
  }
  