content: package com.lgp.netty.netty_ch6;
  
  import io.netty.bootstrap.ServerBootstrap;
  import io.netty.channel.Channel;
  import io.netty.channel.ChannelFuture;
  import io.netty.channel.ChannelInitializer;
  import io.netty.channel.EventLoopGroup;
  import io.netty.channel.group.ChannelGroup;
  import io.netty.channel.group.DefaultChannelGroup;
  import io.netty.channel.nio.NioEventLoopGroup;
  import io.netty.channel.socket.nio.NioServerSocketChannel;
  import io.netty.util.concurrent.ImmediateEventExecutor;
  
  import java.net.InetSocketAddress;
  
  /**
   * @AUTHOR lgp
   * @DATE 2019/1/2 17:17
   * @DESCRIPTION
   **/
  public class ChatServer {
      //1 创建 DefaultChannelGroup 用来 保存所有连接的的 WebSocket channel
      private final ChannelGroup channelGroup = new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE);
      private final EventLoopGroup group = new NioEventLoopGroup();
      private Channel channel;
  
      public ChannelFuture start(InetSocketAddress address) {
          //2 引导 服务器
          ServerBootstrap bootstrap = new ServerBootstrap();
          bootstrap.group(group)
                  .channel(NioServerSocketChannel.class)
                  .childHandler(createInitializer(channelGroup));
          ChannelFuture future = bootstrap.bind(address);
          future.syncUninterruptibly();
          channel = future.channel();
          return future;
      }
      //3 创建 ChannelInitializer
      protected ChannelInitializer<Channel> createInitializer(ChannelGroup group) {
          return new ChatServerInitializer(group);
      }
      //4 .处理服务器关闭，包括释放所有资源
      public void destroy() {
          if (channel != null) {
              channel.close();
          }
          channelGroup.close();
          group.shutdownGracefully();
      }
  
      public static void main(String[] args) throws Exception {
  //        if (args.length != 1) {
  //            System.err.println("Please give port as argument");
  //            System.exit(1);
  //        }
          int port = 11111;
  
          final ChatServer endpoint = new ChatServer();
          ChannelFuture future = endpoint.start(new InetSocketAddress(port));
  
          Runtime.getRuntime().addShutdownHook(new Thread(() -> endpoint.destroy()));
          future.channel().closeFuture().syncUninterruptibly();
      }
  }
  