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


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

一.簡單說明

  筆者有意將Netty做成一個系列的文章,因為筆者並不是一個善於寫文章的人,而且筆者學習很多技術一貫的習慣就是敲代碼,很多東西敲着敲着就就熟了,然后再進行深入的研究,當然這種學習的方式對於有些朋友來講,可能覺得不可思議,但是我想說的是,這只是筆者自己的學習方式而已,我並沒有想把這種學習方式強加給任何人。細心的讀者可能已經發現,其實Netty程序的編寫風格很雷同,不同的可能就是Handler的處理。本文筆者將展示一個基於Netty的多客戶端聊天室的簡單demo,那么我們直接上代碼吧。

二.聊天室的實現

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處理代碼

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客戶端啟動代碼

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客戶端通道初始化代碼

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代碼

public class ClientHandler extends SimpleChannelInboundHandler<String>{

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

三.程序運行

運行服務端啟動代碼,然后運行多個客戶端啟動代碼,接着你就可以聊天了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM