Netty群聊系统

要求:
1、实现客户端与服务器端之间的数据通讯(非阻塞)
2、多人群聊
3、服务器端监测客户端用户上线、离线、并实现消息转发
4、客户端可以通过channel无阻塞发送消息给其他用户,同时可以接收到其他用户发来的消息(由服务器进行转发)

服务器端

public class GroupChatServer {
    private int port; //监听端口
    public GroupChatServer(int port) {
        this.port = port;
    }
    //处理客户端请求
    public void run() throws Exception {
        //创建线程组
        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
        NioEventLoopGroup workerGroup = new NioEventLoopGroup(); //默认大小为cpu核数 * 2
        try{
            //创建服务器引导启动器
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true) //活跃状态
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            //获取到pipeline
                            ChannelPipeline pipeline = ch.pipeline();
                            //往pipeline里面添加处理器
                            //1、向pipeline添加一个解码器
                            pipeline.addLast("decoder", new StringDecoder());
                            //2、向pipeline添加一个编码器
                            pipeline.addLast("encoder", new StringEncoder());
                            //加入自己的业务处理handler
                            pipeline.addLast(new GroupChatServerHandler());
                        }
                    });
            System.out.println("Netty服务器启动...");
            ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
            //监听关闭事件
            channelFuture.channel().closeFuture().sync();
        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
}
    public static void main(String[] args) throws Exception {
        new GroupChatServer(9999).run();
    }
}

服务器Handler

public class GroupChatServerHandler extends SimpleChannelInboundHandler<String> {
    //定义一个channel组,管理所有channel
    //GlobalEventExecutor.INSTANCE 是一个全局的事件执行器(单例)
    private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
    //用于输出时间
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    //handlerAdded 表示链接建立,一旦链接,第一个被执行的方法
    //将当前channel加入channelGroup中进行管理
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        //将该客户加入聊天室的信息推送给其他在线的客户端
        /**
         *  channelGroup中的writeAndFlush 方法会交channelGroup中所有管理的channel进行遍历并发送消息
         *  无需自己再遍历
         */
        channelGroup.writeAndFlush("[客户端]" + channel.remoteAddress() + "加入聊天室  "+sdf.format(new Date()) +"\n");
        channelGroup.add(channel);
    }
    //表示channel处于活动状态、提示 xx上线
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(ctx.channel().remoteAddress()+"上线了...");
    }
    //表示channel处于非活动状态触发
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        channelGroup.writeAndFlush("[客户端]" + channel.remoteAddress() + "离开聊天室  "+sdf.format(new Date()) +"\n");
        System.out.println("当前聊天室人数(ChannelGroup Size):"+channelGroup.size());
    }
    //读取数据
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        //获取当前channel
        Channel channel = ctx.channel();

        //遍历channelGroup,根据不同情况 回送不同消息
        channelGroup.forEach(ch -> {
            if (channel != ch) { //直接转发消息到其他channel
                ch.writeAndFlush("[客户端]" + channel.remoteAddress() + "发送了消息 :" + msg + "  "+sdf.format(new Date()) +"\n");
            } else { //回显自己发送的消息
                ch.writeAndFlush("[自己]发送了消息" + msg + "  "+sdf.format(new Date()) + "\n");
            }
        });
    }
    //发生异常处理
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //关闭当前通道
        ctx.close();
    }
}

客户端

public class GroupChatClient {

    private final String host;
    private final int port;

    public GroupChatClient(String host, int port) {
        this.host=host;
        this.port = port;
    }
    public void run() throws Exception{
        NioEventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap()
                    .group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
                            pipeline.addLast("decoder", new StringDecoder())
                                    .addLast("encoder", new StringEncoder())
                                    .addLast(new GroupChatClientHandler());//加入自定义handler
                        }
                    });
            ChannelFuture channelFuture = bootstrap.connect(host, port).sync();
            Channel channel = channelFuture.channel();
            System.out.println("--------"+channel.localAddress()+"---------");
            //客户端需要输入信息
            Scanner scanner = new Scanner(System.in);
            while (scanner.hasNextLine()) {
                String msg = scanner.nextLine();
                //通过channel发送到服务器端
                channel.writeAndFlush(msg + "\r\n");
            }
        }finally {
            group.shutdownGracefully();
        }
    }
    public static void main(String[] args) throws Exception {
        new GroupChatClient("127.0.0.1", 9999).run();
	}
}

客户端Handler

public class GroupChatClientHandler extends SimpleChannelInboundHandler<String> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.println(msg.trim());
    }
}

效果图:
服务器端检测上线:在这里插入图片描述
客户端发送消息:
在这里插入图片描述
其余客户端接收消息:
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值