content: package com.kefu.netty.handler;
  
  import com.kefu.netty.protocol.request.ListGroupMembersRequestPacket;
  import com.kefu.netty.protocol.response.ListGroupMembersResponsePacket;
  import com.kefu.netty.session.Session;
  import com.kefu.netty.util.SessionUtil;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import io.netty.channel.Channel;
  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 ListGroupMembersRequestHandler extends SimpleChannelInboundHandler<ListGroupMembersRequestPacket> {
  
      public static final ListGroupMembersRequestHandler INSTANCE = new ListGroupMembersRequestHandler();
  
      @Override
      protected void channelRead0(ChannelHandlerContext ctx, ListGroupMembersRequestPacket msg) throws Exception {
          // 获取群的 ChannelGroup
          String groupId = msg.getGroupId();
          ChannelGroup channelGroup = SessionUtil.getChannelGroup(groupId);
  
          // 遍历群成员的 channel，对应的 session，构造群成员的信息
          List<Session> sessionList = new ArrayList<>();
          for (Channel channel : channelGroup) {
              Session session = SessionUtil.getSession(channel);
              sessionList.add(session);
          }
  
          // 构造获取群成员列表响应客户端
          ListGroupMembersResponsePacket listGroupMembersResponsePacket = new ListGroupMembersResponsePacket();
          listGroupMembersResponsePacket.setGroupId(groupId);
          listGroupMembersResponsePacket.setSessionList(sessionList);
          ctx.channel().writeAndFlush(listGroupMembersResponsePacket);
      }
  }
  