Netty之多用户的聊天室(三)

Netty之多用户的聊天室(三)bootstrap

一.简单说明服务器

  笔者有意将Netty作成一个系列的文章,由于笔者并非一个善于写文章的人,并且笔者学习不少技术一向的习惯就是敲代码,不少东西敲着敲着就就熟了,而后再进行深刻的研究,固然这种学习的方式对于有些朋友来说,可能以为难以想象,可是我想说的是,这只是笔者本身的学习方式而已,我并无想把这种学习方式强加给任何人。细心的读者可能已经发现,其实Netty程序的编写风格很雷同,不一样的可能就是Handler的处理。本文笔者将展现一个基于Netty的多客户端聊天室的简单demo,那么咱们直接上代码吧。ide

二.聊天室的实现oop

2.1服务端启动程序学习

public class ChartServer {
    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        
        try{
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            
            serverBootstrap.group(bossGroup, workerGroup)
                           .channel(NioServerSocketChannel.class)
                           .childHandler(new ChartServerInitializer());
            
            ChannelFuture channelFuture = serverBootstrap.bind(8877).sync();
            channelFuture.channel().closeFuture().sync();
        }finally{
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

2.2服务端通道初始化代码编码

public class ChartServerInitializer extends ChannelInitializer<SocketChannel>{

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        
        /**
         * DelimiterBasedFrameDecoder: 基于分隔符的帧解码器。
         * 两个参数的含义分别为:
         *     1.帧数据的最大长度。
         *  2.以XX做为分隔符, Delimiters.lineDelimiter()表示一\r\n或者\n做为分割符号。
         *  例如一下字符串:
         *  +--------------+
         *  | ABC\nDEF\r\n |
         *  +--------------+
         *  解码后的字符串为:
         *  +-----+-----+
         *  | ABC | DEF |
         *  +-----+-----+
         *  而不是:
         *  +----------+
         *  | ABC\nDEF |
         *  +----------+
         */
        pipeline.addLast("delimiterBasedFrameDecoder", new DelimiterBasedFrameDecoder(4096, Delimiters.lineDelimiter()));
        pipeline.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8));  //编码不指定,默认为utf-8
        pipeline.addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8));
        pipeline.addLast(new ChartServerHandler());
    }
}

2.3服务端Handler处理代码spa

public class ChartServerHandler extends SimpleChannelInboundHandler<String>{

    private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);    
    
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        Channel channel = ctx.channel();
        System.out.println(channelGroup.size());
        channelGroup.forEach(c -> {
            if(channel == c){  //若是本身
                channel.writeAndFlush("【本身】: " + msg + "\n");
            }else{ //发送给其余人的信息
                c.writeAndFlush(channel.remoteAddress() + ": " + msg + "\n");
            }
        });
    }

    //链接创建
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel(); //获取到上线的链接
        
        channelGroup.writeAndFlush("【服务器】: " + channel.remoteAddress() + "上线\n"); //服务器发送广播通知
        
        channelGroup.add(channel);  //将通道添加到组
    }
    
    //链接断开
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        channelGroup.writeAndFlush("【服务器】: " + channel.remoteAddress() + "下线了\n");
    };
    
    //链接激活
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(ctx.channel().remoteAddress() + "上线了");
    }
    
    //链接断开
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(ctx.channel().remoteAddress() + "下线了");
    };
    
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}

2.4客户端启动代码code

public class Client {
    public static void main(String[] args) throws InterruptedException, IOException {
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        
        try{
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(eventLoopGroup)
                     .channel(NioSocketChannel.class)
                     .handler(new ClientInitializer());
            
            Channel channel = bootstrap.connect("localhost", 8877).sync().channel();
            
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            for(;;){
                channel.writeAndFlush(reader.readLine() + "\n");
            }
        }finally{
            eventLoopGroup.shutdownGracefully(); //优雅关闭
        }
    }
}

2.5客户端通道初始化代码server

public class ClientInitializer extends ChannelInitializer<SocketChannel>{

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        
        pipeline.addLast("delimiterBasedFrameDecoder", new DelimiterBasedFrameDecoder(4096, Delimiters.lineDelimiter()));
        pipeline.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8));
        pipeline.addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8));
        
        pipeline.addLast(new ClientHandler());
    }
}

2.6.客户端Handler代码blog

public class ClientHandler extends SimpleChannelInboundHandler<String>{

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.println(msg);
    }
}

三.程序运行

运行服务端启动代码,而后运行多个客户端启动代码,接着你就能够聊天了。

相关文章
相关标签/搜索