package com.xjs.netty.chat;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * 聊天室业务处理类
 * @author xiejs
 * @since 2022-03-09
 */
public class NettyChatServerHandler extends SimpleChannelInboundHandler<String> {

    private static List<Channel> channelList = new ArrayList<>();

    /**
     * 通道就绪事件
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        Channel channel = ctx.channel();

        //当有新的客户端连接的时候，将通道放入集合
        channelList.add(channel);

        System.out.println("[Server]:"+channel.remoteAddress().toString().substring(1)+"在线");

    }

    /**
     * 通道读取事件
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        //当前发送消息的通道，当前发送的客户端连接
        Channel channel = ctx.channel();

        for (Channel channel1 : channelList) {
            //排除自身通道
            if (channel != channel1) {
                channel1.writeAndFlush("[" + channel.remoteAddress().toString().substring(1) + "]说：" + msg);
            }
        }

    }

    /**
     * 通道未就绪 --channel下线
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {

        Channel channel = ctx.channel();

        //当有客户端断开连接的时候，就移出对应的通道
        channelList.remove(channel);

        System.out.println("[Server]"+channel.remoteAddress().toString().substring(1)+"下线");

    }


    /**
     * 异常处理事件
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        Channel channel = ctx.channel();
        System.out.println("[Server]:"+channel.remoteAddress().toString().substring(1)+"异常");
    }
}

