Netty实现群聊demo

Netty实现群聊demo

一、pom引入

		<dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.42.Final</version>
        </dependency>

二、服务端

1、服务端启动类
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import java.awt.*;

public class GroupChatServer {

    /**
     * 端口
     */
    private int port;


    public GroupChatServer(int port) {
        this.port = port;
    }

    public void run() {

        //处理连接线程组
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();

        //处理事务线程组
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        //Netty服务启动对象
        ServerBootstrap serverBootstrap = new ServerBootstrap();

        //Netty服务配置
        //设置线程组
        serverBootstrap.group(bossGroup, workerGroup)
                //设置通道实现
                .channel(NioServerSocketChannel.class)
                //设置线程队列得到连接个数
                .option(ChannelOption.SO_BACKLOG, 128)
                //设置保持活动连接状态
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                //给workerGroup的NioEventLoop设置对应的通道处理器
                .childHandler(new GroupChatServerChannelInitializer());

        System.out.println("群聊服务器启动...");

        try {

            //启动服务器(并绑定端口)
            ChannelFuture channelFuture = serverBootstrap.bind(6666).sync();

            //对关闭通道进行监听
            channelFuture.channel().closeFuture().sync();

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            //关闭线程组
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();

        }


    }


    /**
     * 启动
     * @param args
     */
    public static void main(String[] args) {
        new GroupChatServer(6666).run();
    }

}

2、服务端通道配置类
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

public class GroupChatServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {

        //获取管道
        ChannelPipeline channelPipeline = socketChannel.pipeline();

        //字符串解码器
        channelPipeline.addLast("stringDecoder",new StringDecoder());
        //字符串编码器
        channelPipeline.addLast("stringEncoder",new StringEncoder());
        //信息读取处理器
        channelPipeline.addLast("groupChatServerInputMessageHandler",new GroupChatServerInputMessageHandler());

    }
}

3、服务端信息处理类
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.EventExecutor;
import io.netty.util.concurrent.GlobalEventExecutor;
import java.text.SimpleDateFormat;
import java.util.Date;

public class GroupChatServerInputMessageHandler extends SimpleChannelInboundHandler<String> {


    /**
     * GlobalEventExecutor.INSTANCE  全局事件执行器
     */
    private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);


    /**
     * 时间格式化
     */
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");



    /**
     *
     * 通道处于活动状态
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("["+simpleDateFormat.format(new Date())+"][客户端][" + ctx.channel().remoteAddress() + "]——上线");
    }


    /**
     *
     * 通道处于不活动状态
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("["+simpleDateFormat.format(new Date())+"][客户端][" + ctx.channel().remoteAddress() + "]——下线");
    }


    /**
     *
     *  客户端连接上服务器后,执行该方法
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        //获取通道
        Channel channel = ctx.channel();
        /**
         * channelGroup.writeAndFlush(msg)方法会遍历channelGroup中的通道,然后向他们发送信息
         */
        channelGroup.writeAndFlush("["+simpleDateFormat.format(new Date())+"][客户端][" + channel.remoteAddress() + "]——加入聊天\n");
        //添加通道到channelGroup
        channelGroup.add(channel);
    }




    /**
     *
     * 客户端与服务器断开连接后,执行该方法
     *
     * 通道断开后,会自动从channelGroup中移除
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        //获取通道
        Channel channel = ctx.channel();
        /**
         * channelGroup.writeAndFlush(msg)方法会遍历channelGroup中的通道,然后向他们发送信息
         */
        channelGroup.writeAndFlush("["+simpleDateFormat.format(new Date())+"][客户端][" + channel.remoteAddress() + "]——离开聊天\n");

    }


    /**
     * 读取通道数据
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        Channel channel = ctx.channel();
        channelGroup.forEach(c -> {
            if (channel != c){
                c.writeAndFlush("["+simpleDateFormat.format(new Date())+"][客户端][" + channel.remoteAddress() + "]:" + msg + "\n");
            }else{
                c.writeAndFlush("["+simpleDateFormat.format(new Date())+"][自己]" + ":" + msg + "\n");
            }
        });
    }


    /**
     * 异常处理(关闭通道)
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
    }
}

三、客户端

1、客户端启动类

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.Scanner;


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() {

        //客户端只需要一个事件循环组
        NioEventLoopGroup eventLoop = new NioEventLoopGroup();

        //创建客户端启动对象
        Bootstrap bootstrap = new Bootstrap();

        try {
            //客户端启动对象配置
            //设置线程组
            bootstrap.group(eventLoop)
                    //设置客户端通道的实现类
                    .channel(NioSocketChannel.class)
                    //给事件循环组设置对应的通道处理器
                    .handler(new GroupChatClientChannelInitializer());

            //连接服务器
            ChannelFuture channelFuture = bootstrap.connect(host, port).sync();

            //获取通道
            Channel channel = channelFuture.channel();

            //键盘输入对象
            Scanner scanner = new Scanner(System.in);

            //信息输入
            while (scanner.hasNextLine()) {

                String msg = scanner.nextLine();

                if (msg.equals("exit")) {
                    break;
                }

                //信息发送
                channel.writeAndFlush(msg + "\r\n");

            }

            //关闭通道进行监听
            channel.closeFuture().sync();

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            //关闭事件循环组
            eventLoop.shutdownGracefully();

        }

    }


    /**
     * 运行
     * @param args
     */
    public static void main(String[] args) {
        new GroupChatClient("127.0.0.1",6666).run();
    }

}

2、客户端通道配置类

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;


public class GroupChatClientChannelInitializer extends ChannelInitializer<SocketChannel> {


    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        //获取管道
        ChannelPipeline channelPipeline = socketChannel.pipeline();

        //字符串解码器
        channelPipeline.addLast("stringDecoder",new StringDecoder());
        //字符串编码器
        channelPipeline.addLast("stringEncoder",new StringEncoder());
        //信息读取处理器
        channelPipeline.addLast("groupChatClientInputMessageHandler",new GroupChatClientInputMessageHandler());

    }

}

3、客户端信息处理类

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

public class GroupChatClientInputMessageHandler  extends SimpleChannelInboundHandler<String> {


    /**
     * 读取通道信息
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.println(msg.trim());
    }


}

四、运行截图

1、服务端

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bNDL75EA-1637738040299)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20211124151237614.png)]

2、客户端1

3、客户端2

在这里插入图片描述

4、客户端3

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大树下躲雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值