最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

從入門到超神進(jìn)階的Netty群聊系統(tǒng)

 更新時(shí)間:2021年08月19日 15:35:56   作者:墨家巨子@俏如來(lái)  
本篇文章基于Netty做一個(gè)聊天室案例加強(qiáng)Netty的熟練度,案例的效果是服務(wù)端可以廣播某客戶端的消息給所有客戶端。每個(gè)客戶端監(jiān)聽(tīng)鍵盤輸入來(lái)獲取消息,然后發(fā)送給服務(wù)端

服務(wù)端

服務(wù)端一樣的需要?jiǎng)?chuàng)建BossGroup 和 WorkGroup , 然后使用ServerBootStrap 來(lái)配置Netty和啟動(dòng)Netty。

public class NettyGroupChatServer {
    public static void main(String[] args) {
        new NettyGroupChatServer().start();
    }
    //監(jiān)聽(tīng)
   public void start(){
        //循環(huán)組
       NioEventLoopGroup bossGroup = new NioEventLoopGroup();
       NioEventLoopGroup workGroup = new NioEventLoopGroup();
       //啟動(dòng)引導(dǎo)
       ServerBootstrap bootstrap = new ServerBootstrap();
       //Netty配置
       bootstrap.group(bossGroup,workGroup)
               .option(ChannelOption.SO_BACKLOG,32)
               .childOption(ChannelOption.SO_KEEPALIVE,true)
               .channel(NioServerSocketChannel.class)
               .childHandler(new ChannelInitializer<SocketChannel>() {
                   @Override
                   protected void initChannel(SocketChannel channel) throws Exception {
                       //解碼器
                       channel.pipeline().addLast("decoder", new StringDecoder());
                       //編碼器
                       channel.pipeline().addLast("encoder",new StringEncoder());
                       //處理器
                       channel.pipeline().addLast("nettyGroupChatHandler",new NettyGroupChatServerHandler());
                   }
               });
       try {
           //啟動(dòng)服務(wù)
           ChannelFuture future = bootstrap.bind(new InetSocketAddress("127.0.0.1", 8888)).sync();
           future.channel().closeFuture().sync();
       } catch (InterruptedException e) {
           e.printStackTrace();
       }finally {
           //關(guān)閉資源
           bossGroup.shutdownGracefully();
           workGroup.shutdownGracefully();
       }
   }
}

服務(wù)端處理器我們通過(guò)繼承 SimpleChannelInboundHandler 入站Handler來(lái)處理消息。

其中提供了幾個(gè)方法

  • channelRead0 ():讀取消息
  • handlerRemoved ():客戶端斷開(kāi)
  • handlerAdded ():客戶端建立連接
  • exceptionCaught ():出現(xiàn)異常

具體代碼如下

public class NettyGroupChatServerHandler extends SimpleChannelInboundHandler<String> {
    //把所有客戶端的channel保存起來(lái)
    private static ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        String message = dateFormat.format(new Date())+":%s:"+msg;
        //消息轉(zhuǎn)發(fā)給所有的客戶端
        channels.forEach(channel -> {
            if(channel == ctx.channel()){
                String sendMsg = String.format(message, "我");
                channel.writeAndFlush(sendMsg);
                System.out.println(sendMsg);

            }else{
                String sendMsg = String.format(message, ctx.channel().remoteAddress());
                channel.writeAndFlush(sendMsg);
                System.out.println(sendMsg);
            }
        });
    }
    //斷開(kāi)連接 , 把消息廣播給其他客戶端
    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        String message = dateFormat.format(new Date())+":"+ctx.channel().remoteAddress()+":斷開(kāi)連接";
        channels.writeAndFlush(message);
        System.out.println(message);
    }
    //建立連接
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        String message = dateFormat.format(new Date())+":"+ctx.channel().remoteAddress()+":加入聊天室";
        //自動(dòng)把消息廣播給其客戶端
        channels.writeAndFlush(message);
        //客戶端加入組
        channels.add(ctx.channel());
        System.out.println(message);
    }
    //出現(xiàn)異常
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.channel().close();
    }
    //客戶端退出
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        String message = dateFormat.format(new Date())+":"+ctx.channel().remoteAddress()+":退出聊天室";
        System.out.println(message);
    }
    //客戶端出于活動(dòng)
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        String message = dateFormat.format(new Date())+":"+ctx.channel().remoteAddress()+":上線啦";
        System.out.println(message);
    }
}

客戶端

客戶端需要?jiǎng)?chuàng)建一個(gè)循環(huán)事件組,然后通過(guò)BootStrap去啟動(dòng),然后掃描鍵盤輸入作為數(shù)據(jù)源來(lái)把信息發(fā)送給服務(wù)端

public class NettyGroupChatClient {
    public static void main(String[] args) {
        new NettyGroupChatClient().start();
    }
    public void start(){
        NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(eventLoopGroup)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel channel) throws Exception {
                        //解碼器
                        channel.pipeline().addLast("decoder", new StringDecoder());
                        //編碼器
                        channel.pipeline().addLast("encoder",new StringEncoder());
                        //處理器
                        channel.pipeline().addLast("nettyGroupChatClientHandler",new NettyGroupChatClientHandler());
                    }
                });
        try {
            ChannelFuture future = bootstrap.connect(new InetSocketAddress("127.0.0.1", 8888)).sync();
            //通道
            Channel channel = future.channel();
            //掃描鍵盤輸入
            Scanner scanner = new Scanner(System.in);
            while(scanner.hasNextLine()){
                String message = scanner.nextLine();
                //發(fā)送數(shù)據(jù)
                channel.writeAndFlush(message);
            }
        } catch (InterruptedException e) {
        }finally {
            eventLoopGroup.shutdownGracefully();
        }
    }
}

對(duì)于處理器只需要接收服務(wù)端轉(zhuǎn)發(fā)過(guò)來(lái)的消息即可

public class NettyGroupChatClientHandler extends SimpleChannelInboundHandler<String> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.println(msg);
    }
}

啟動(dòng)服務(wù)端和多個(gè)客戶端,效果如下

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

到此這篇關(guān)于從入門到超神系列的Netty群聊系統(tǒng)的文章就介紹到這了,更多相關(guān)Netty群聊系統(tǒng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JRebel2023.3 插件使用及安裝步驟詳解

    JRebel2023.3 插件使用及安裝步驟詳解

    JRebel是一款JVM插件,它使得Java代碼修改后不用重啟系統(tǒng),立即生效,IDEA上原生是不支持熱部署的,一般更新了 Java 文件后要手動(dòng)重啟 Tomcat 服務(wù)器,才能生效,浪費(fèi)時(shí)間浪費(fèi)生命,目前對(duì)于idea熱部署最好的解決方案就是安裝JRebel插件,本文分步驟介紹的非常詳細(xì),一起看看吧
    2023-08-08
  • Java如何獲取Cookie和Session

    Java如何獲取Cookie和Session

    Cookie?和?Session之間主要是通過(guò)?SessionId?關(guān)聯(lián)起來(lái)的,?SessionId是?Cookie?和?Session?之間的橋梁,這篇文章主要介紹了Java獲取Cookie和Session的方法,需要的朋友可以參考下
    2024-01-01
  • lombok注解介紹小結(jié)

    lombok注解介紹小結(jié)

    lombok是一個(gè)可以幫助我們簡(jiǎn)化java代碼編寫的工具類,這篇文章主要介紹了lombok注解介紹小結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • 如何使用 IntelliJ IDEA 編寫 Spark 應(yīng)用程序(Scala + Maven)

    如何使用 IntelliJ IDEA 編寫 Spark 應(yīng)用程序(Sc

    本教程展示了如何在IntelliJIDEA中使用Maven編寫和運(yùn)行一個(gè)簡(jiǎn)單的Spark應(yīng)用程序(例如WordCount程序),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-11-11
  • SpringBoot實(shí)現(xiàn)異步事件Event詳解

    SpringBoot實(shí)現(xiàn)異步事件Event詳解

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)異步事件Event詳解,異步事件的模式,通常將一些非主要的業(yè)務(wù)放在監(jiān)聽(tīng)器中執(zhí)行,因?yàn)楸O(jiān)聽(tīng)器中存在失敗的風(fēng)險(xiǎn),所以使用的時(shí)候需要注意,需要的朋友可以參考下
    2023-11-11
  • java將m3u8格式轉(zhuǎn)成視頻文件的方法

    java將m3u8格式轉(zhuǎn)成視頻文件的方法

    這篇文章主要介紹了如何java將m3u8格式轉(zhuǎn)成視頻文件,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Mybatis傳遞多個(gè)參數(shù)的三種實(shí)現(xiàn)方法

    Mybatis傳遞多個(gè)參數(shù)的三種實(shí)現(xiàn)方法

    這篇文章主要介紹了Mybatis傳遞多個(gè)參數(shù)的三種實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Intellij IDEA 最全超實(shí)用快捷鍵整理(長(zhǎng)期更新)

    Intellij IDEA 最全超實(shí)用快捷鍵整理(長(zhǎng)期更新)

    這篇文章主要介紹了Intellij IDEA 最全實(shí)用快捷鍵整理(長(zhǎng)期更新),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • SpringBoot 集成 Nebula的操作過(guò)程

    SpringBoot 集成 Nebula的操作過(guò)程

    這篇文章主要介紹了SpringBoot 集成 Nebula的操作過(guò)程,通過(guò)示例代碼介紹了java 環(huán)境下如何對(duì) Nebula Graph 進(jìn)行操作,感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • idea tomcat亂碼問(wèn)題的解決及相關(guān)設(shè)置的步驟

    idea tomcat亂碼問(wèn)題的解決及相關(guān)設(shè)置的步驟

    這篇文章主要介紹了idea tomcat亂碼問(wèn)題的解決及相關(guān)設(shè)置的步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11

最新評(píng)論

虞城县| 两当县| 故城县| 周宁县| 峡江县| 镇江市| 漯河市| 华阴市| 林甸县| 富民县| 克东县| 运城市| 利津县| 绩溪县| 深圳市| 新乡县| 穆棱市| 屏东县| 密山市| 柘城县| 宁波市| 华亭县| 恩平市| 兰西县| 博爱县| 湟源县| 板桥市| 玉树县| 清流县| 无锡市| 枣阳市| 弋阳县| 彰化市| 教育| 鹤峰县| 冕宁县| 花垣县| 纳雍县| 余姚市| 涞水县| 苏尼特左旗|