content: package com.kefu.netty.handler;
  
  import com.kefu.netty.protocol.request.QuitGroupRequestPacket;
  import com.kefu.netty.protocol.response.QuitGroupResponsePacket;
  import com.kefu.netty.util.SessionUtil;
  
  import io.netty.channel.ChannelHandler;
  import io.netty.channel.ChannelHandlerContext;
  import io.netty.channel.SimpleChannelInboundHandler;
  import io.netty.channel.group.ChannelGroup;
  
  /**
   * @author feng
   * @date 2019-04-21
   */
  @ChannelHandler.Sharable
  public class QuitGroupRequestHandler extends SimpleChannelInboundHandler<QuitGroupRequestPacket> {
  
      public static final QuitGroupRequestHandler INSTANCE = new QuitGroupRequestHandler();
  
      @Override
      protected void channelRead0(ChannelHandlerContext ctx, QuitGroupRequestPacket msg) throws Exception {
          // 获取群组对应的 channelGroup，然后将当期用户的 channel 移除
          String groupId = msg.getGroupId();
          ChannelGroup channelGroup = SessionUtil.getChannelGroup(groupId);
          channelGroup.remove(ctx.channel());
  
          // 构造退出群组的响应数据包发送给客户端
          QuitGroupResponsePacket quitGroupResponsePacket = new QuitGroupResponsePacket();
          quitGroupResponsePacket.setGroupId(groupId);
          quitGroupResponsePacket.setSuccess(true);
          ctx.channel().writeAndFlush(quitGroupResponsePacket);
      }
  }
  