Netty实战 IM即时通讯系统(十一)pipeline与channelHandler
生活随笔
收集整理的這篇文章主要介紹了
Netty实战 IM即时通讯系统(十一)pipeline与channelHandler
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Netty實戰 IM即時通訊系統(十一)pipeline與channelHandler
零、 目錄
- Netty 簡介
- Netty 環境配置
- 服務端啟動流程
- 客戶端啟動流程
- 實戰: 客戶端和服務端雙向通信
- 數據傳輸載體ByteBuf介紹
- 客戶端與服務端通信協議編解碼
- 實現客戶端登錄
- 實現客戶端與服務端收發消息
- pipeline與channelHandler
- 構建客戶端與服務端pipeline
- 拆包粘包理論與解決方案
- channelHandler的生命周期
- 使用channelHandler的熱插拔實現客戶端身份校驗
- 客戶端互聊原理與實現
- 群聊的發起與通知
- 群聊的成員管理(加入與退出,獲取成員列表)
- 群聊消息的收發及Netty性能優化
- 心跳與空閑檢測
- 總結
- 擴展
一、 簡介
二、 pipeline 與channelHandler 的構成
三、 ChannelHandler 的分類
四、 ChannelInboundHanndler 的事件傳播
關于ChannelInboundHandler , 我們拿 ChannelRead() 為例子 , 來體驗一下inbound時間的傳播
我們在服務端的pipeline 添加三個ChannelInboundHandler
Test_12_Server.javaserverBootstrap.group(bossGroup, workGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<NioSocketChannel>() {@Overrideprotected void initChannel(NioSocketChannel ch) throws Exception {ch.pipeline().addLast(new Test_11_InboundHandlerA());ch.pipeline().addLast(new Test_11_InboundHandlerB());ch.pipeline().addLast(new Test_11_InboundHandlerC());}});每個inBoundHandler 繼承自ChannelInboundHandlerAdapter , 然后實現了 channelRead() 方法
/*** 2019年1月29日* @author outman* 服務端處理 A*/class Test_11_InboundHandlerA extends ChannelInboundHandlerAdapter{@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {ByteBuf buffer = (ByteBuf)msg;System.out.println("Test_11_InboundHandlerA --> " + buffer.toString(Charset.forName("UTF-8")));super.channelRead(ctx, msg);}}/*** 2019年1月29日* @author outman* 服務端處理 B*/class Test_11_InboundHandlerB extends ChannelInboundHandlerAdapter{@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {ByteBuf buffer = (ByteBuf)msg;System.out.println("Test_11_InboundHandlerB --> " + buffer.toString(Charset.forName("UTF-8")));super.channelRead(ctx, msg);}}/*** 2019年1月29日* @author outman* 服務端處理 C*/class Test_11_InboundHandlerC extends ChannelInboundHandlerAdapter{@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {ByteBuf buffer = (ByteBuf)msg;System.out.println("Test_11_InboundHandlerC --> " + buffer.toString(Charset.forName("UTF-8")));super.channelRead(ctx, msg);}}五、ChannelOutboundHandler 的時間傳播
關于ChannelOutboundHandler , 我們拿write()為例子,來體驗一下outbound事件的傳播。
我們繼續在服務端的pipeline添加三個ChannelOutboundHandler
Test_12_Server.javaserverBootstrap.group(bossGroup, workGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<NioSocketChannel>() {@Overrideprotected void initChannel(NioSocketChannel ch) throws Exception {// inbound 服務端讀數據邏輯ch.pipeline().addLast(new Test_11_InboundHandlerA());ch.pipeline().addLast(new Test_11_InboundHandlerB());ch.pipeline().addLast(new Test_11_InboundHandlerC());// outbound 服務端系數據邏輯ch.pipeline().addLast(new Test_11_OutboundHandlerA());ch.pipeline().addLast(new Test_11_OutboundHandlerB());ch.pipeline().addLast(new Test_11_OutboundHandlerC());}});每個outboundHandler都繼承自ChannelOutboundHandlerAdapter , 然后實現了write()方法
/*** 2019年2月14日* @author outman** 服務端寫數據邏輯 A*/class Test_12_OutboundHandlerA extends ChannelOutboundHandlerAdapter {@Overridepublic void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {System.out.println("Test_12_OutboundHandlerA --> " + msg);super.write(ctx, msg, promise);}}/*** 2019年2月14日* @author outman** 服務端寫數據邏輯 B*/class Test_12_OutboundHandlerB extends ChannelOutboundHandlerAdapter {@Overridepublic void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {System.out.println("Test_12_OutboundHandlerB --> " + msg);super.write(ctx, msg, promise);}}/*** 2019年2月14日* @author outman** 服務端寫數據邏輯 C*/class Test_12_OutboundHandlerC extends ChannelOutboundHandlerAdapter {@Overridepublic void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {System.out.println("Test_12_OutboundHandlerC --> " + msg);super.write(ctx, msg, promise);}}可以看到outboundHandler的執行順序與我們添加的順序相反 , 最后,我們在來看一下pipeline的結構和執行順序。
六 、 總結
七 、 思考
總結
以上是生活随笔為你收集整理的Netty实战 IM即时通讯系统(十一)pipeline与channelHandler的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Netty实战 IM即时通讯系统(十)实
- 下一篇: Netty实战 IM即时通讯系统(十二)