超详细!搭建netty服务器和客户端实现多人聊天。

netty旨在为可维护的高性能、高可扩展性协议服务器和客户端的快速开发提供异步事件驱动的网络应用程序框架和工具。换句话说,Netty是一个NIO客户端服务器框架,可以快速轻松地开发协议服务器和客户端等网络应用程序。它极大地简化并简化了TCP和UDP套接字服务器开发等网络编程。

1.引入jar包

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

2.搭建群聊服务器

/**
 * @Package com.csdn.netty
 * @ClassName ChatServer
 * @Description  群聊服务端
 * @Author LangShengJie
 * @Date Created in 2020/11/25
 */
public class ChatServer {
	//监听的端口号
    private final int port;
    public ChatServer(int port) {
        this.port = port;
    }
    public void start() throws InterruptedException {
        //NioEventLoopGroup是用来处理IO操作的多线程事件循环器
        //parent用来接收进来的连接
        EventLoopGroup parentGroup = new NioEventLoopGroup();
        //child用来处理已经被接收的连接;
        EventLoopGroup childGroup = new NioEventLoopGroup();
        try {
            //是一个启动NIO服务的辅助启动类
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            //为bootstrap设置acceptor的EventLoopGroup和client的EventLoopGroup
            //这些EventLoopGroups用于处理所有的IO事件
            serverBootstrap.group(parentGroup, childGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel sc) throws Exception {
                    ChannelPipeline pipeline = sc.pipeline();
                    //添加一个基于行的解码器
                    pipeline.addLast(new LineBasedFrameDecoder(2048));
                    pipeline.addLast(new StringDecoder());
                    pipeline.addLast(new StringEncoder());
                    pipeline.addLast(new ChatServerHandler());
                }
            }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.TCP_NODELAY, true);
            //绑定端口,开始接收进来的连接
            ChannelFuture future = serverBootstrap.bind(port).sync();
            System.out.println("服务器已启动");
            //等待服务器socket关闭
            future.channel().closeFuture().sync();
        } finally {
            parentGroup.shutdownGracefully();
            childGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new ChatServer(8888).start();
        }
}

Netty自带的事件几乎都在 SimpleChannelInboundHandler 和 ChannelInboundHandlerAdapter 类中。客户端继承SimpleChannelInboundHandler类,服务端继承ChannelInboundHandlerAdapter类。

二者的区别:SimpleChannelInboundHandler在接收到数据后会自动release掉数据占用的Bytebuffer资源(自动调用Bytebuffer.release())。而为何服务器端不能用呢,因为我们想让服务器把客户端请求的数据发送回去,而服务器端有可能在channelRead方法返回前还没有写完数据,因此不能让它自动release。

3.构建ChatServerHandler类继承ChannelInboundHandlerAdapter 重写其中的方法。

/**
 * @Package com.csdn.netty
 * @ClassName ChatServerHandler
 * @Description 处理聊天服务器的各种情况
 * @Author LangShengJie
 * @Date Created in 2020/11/25 13:58
 */
public class ChatServerHandler extends ChannelInboundHandlerAdapter {
    // 创建一个ChannelGroup,其是一个线程安全的集合,其中存放着与当前服务器相连接的所有Active状态的Channel
    // GlobalEventExecutor是一个单例、单线程的EventExecutor,是为了保证对当前group中的所有Channel的处理
    // 线程是同一个线程
    private static ChannelGroup group = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    // 只要有客户端Channel给当前的服务端发送了消息,那么就会触发该方法的执行
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // 获取到向服务器发送消息的channel
        Channel channel = ctx.channel();
        // 这里要实现将消息广播给所有group中的客户端Channel
        // 发送给自己的消息与发送给大家的消息是不一样的
        group.forEach(ch -> {
            if (ch != channel) {
                ch.writeAndFlush(channel.remoteAddress() + ":" + msg + "\n");
            } else {
                channel.writeAndFlush("me:" + msg + "\n");
            }
        });
    }

    // 只要有客户端Channel与服务端连接成功就会执行这个方法
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // 获取到当前与服务器连接成功的channel
        Channel channel = ctx.channel();
        System.out.println(channel.remoteAddress() + "---上线");
        group.writeAndFlush(channel.remoteAddress() + "---上线\n");
        // 将当前channel添加到group中
        group.add(channel);
    }

    // 只要有客户端Channel断开与服务端的连接就会执行这个方法
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        // 获取到当前要断开连接的Channel
        Channel channel = ctx.channel();
        System.out.println(channel.remoteAddress() + "------下线");
        group.writeAndFlush(channel.remoteAddress() + "下线,当前在线人数:" + group.size() + "\n");

        // group中存放的都是Active状态的Channel,一旦某Channel的状态不再是Active,
        // group会自动将其从集合中踢出,所以,下面的语句不用写
        // remove()方法的应用场景是,将一个Active状态的channel移出group时使用
        // group.remove(channel);
    }

    /**
     * 当Channel中的数据在处理过程中出现异常时会触发该方法的执行
     * @param ctx   上下文
     * @param cause 发生的异常对象
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}

4.搭建群聊客户端

/**
 * @Package com.csdn.netty
 * @ClassName ChatClient
 * @Description 群聊客户端
 * @Author LangShengJie
 * @Date Created in 2020/11/25 14:05
 */
public class ChatClient {
        private final String host;
        private final int port;
        public ChatClient(String host, int port) {
            this.host = host;
            this.port = port;
        }
        public void start() {
            NioEventLoopGroup group = new NioEventLoopGroup();
            Bootstrap bootstrap = new Bootstrap();
            try {
                bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(new LineBasedFrameDecoder(2048));
                        pipeline.addLast(new StringDecoder());
                        pipeline.addLast(new StringEncoder());
                        pipeline.addLast(new ChatClientHandler());
                    }
                });
                ChannelFuture future = bootstrap.connect(host, port).sync();
                // 获取键盘输入
                InputStreamReader is = new InputStreamReader(System.in, "UTF-8");
                BufferedReader br = new BufferedReader(is);
                // 将输入的内容写入到Channel
                while (true) {
                    //br.readLine()中执行fill()方法获取输入数据,获取不到时会发生阻塞,直到获取到数据为止
                    future.channel().writeAndFlush(br.readLine() + "\r\n");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                group.shutdownGracefully();
            }
        }
    public static void main(String[] args) {
        new ChatClient("127.0.0.1",8080);
    }
}

5.构建ChatClientHandler类继承SimpleChannelInboundHandler并重写其中的方法。

/**
 * @Package com.csdn.netty
 * @ClassName ChatClientHandler
 * @Description  客户端处理
 * @Author LangShengJie
 * @Date Created in 2020/11/25 14:06
 */
public class ChatClientHandler extends SimpleChannelInboundHandler<String> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
        System.out.println(s);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}

6.最终展示
(1)启动服务端
在这里插入图片描述
(2)启动多个客户端
在这里插入图片描述
(3)聊天内容
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值