尚硅谷2019年Netty教程 netty handler链调用机制升级 ----目标netty---step5.41

本文为下文姊妹篇
https://editor.csdn.net/md/?articleId=108848924
尚硅谷2019年Netty教程 netty handler链调用机制 ----目标netty—step5.40
增加内容

=服务端
01 服务端的 类 MyServerHandler 读取数据方法 channelRead0
在收到客户端发送过来的数据后,写数据到 ByteBuf 中
ctx.writeAndFlush(98765L);
02 所以需要在 类 MyServerInitializer 的方法 initChannel
中增加响应的 出站的handler ,对写向client的数据(98765)进行 编码 ,然后发送
pipeline.addLast(new MyLongToByteEncoder());

=客户端
03 客户端类 MyClientHandler 增加 读取数据的 方法 channelRead0
public class MyClientHandler extends SimpleChannelInboundHandler {
@Override
protected void channelRead0(ChannelHandlerContext ctx, Long msg) throws Exception {
System.out.println("======= step06 服务器ip: "+ ctx.channel().remoteAddress()+ " 服务器消息 : "+ msg);
}
04 因为需要读取数据,所以需要在管道中增加 byteToLong的解码方法
MyClientInitializer initChannel
//这是一个入栈的解码器 handler
pipeline.addLast(new MyByteToLongDecoder());

handler链调用机制示例
https://www.bilibili.com/video/BV1jK4y1s7GV?p=79
https://www.bilibili.com/video/BV1jK4y1s7GV?p=80
https://www.bilibili.com/video/BV1jK4y1s7GV?p=81
https://www.bilibili.com/video/BV1jK4y1s7GV?p=82
https://www.bilibili.com/video/BV1jK4y1s7GV?p=83

socket --> channel -->入栈
channel --> socket --> 出栈

(server)解码器–ProtobufDecoder : MessageToMessageDecoder : ChannelInboundHandlerAdapter
(client)编码器–ProtobufEncoder : MessageToMessageEncoder : ChannelOutboundHandlerAdapter : ChannelHandlerAdapter : ChannelHandler (I)

                        ByteToMessageDecoder 

ByteToMessageDecoder 会对入栈数据进行缓冲,直到它准备好处理。

服务端的channelpipeline接收到客户端通过socket传过来的数据,解析
ToIntegerDecoder
当没有更多元素添加到List的时候,它的内容将会被发送给下一个ChannelInBoundHandler.

读数据(encoder)的顺序
socket channelPipeline
二进制数据 <-- ToIntegerDecoder --> 业务处理器 ChannelInboundHandlerAdapter —>---|
二进制数据 <-- ToIntegerEncoder <-- 业务处理器 ChannelOutboundHandlerAdapter —<---|

在这里插入图片描述在这里插入图片描述

在这里插入图片描述在这里插入图片描述

==========================客户端代码 ======================================


package com.atguigu.inboundhandlerandoutboundhandler.client;

import com.sun.corba.se.impl.orbutil.concurrent.Sync;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * Author: tz_wl
 * Date: 2020/9/28 15:50
 * Content:
 */
public class MyClient {
   
    public static void main(String[] args) throws Exception {
   
        EventLoopGroup group = new NioEventLoopGroup();

        try {
   
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new MyClientInitializer());

            ChannelFuture channelFuture = bootstrap.connect("localhost", 8087).sync
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值