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);
    }
}

三.程序運行

運行服務端啓動代碼,而後運行多個客戶端啓動代碼,接着你就能夠聊天了。

相關文章
相關標籤/搜索