关键代码如下
p.addLast(new ProtobufVarint32FrameDecoder());// 解码1. 根据byte 的头长度 来 分割
p.addLast(new ProtobufDecoder( Msg.Person.getDefaultInstance())); //解码2。 byte 转化为 实体类
p.addLast(new ProtobufVarint32LengthFieldPrepender()); //编码3. byte 数组头加上 ,实体类的长度
p.addLast(new ProtobufEncoder()); //编码2. Msg.Person 实体 转化为 byte数组
p.addLast( new EchoServerHandler()); //编码1. 写入 Msg.Person 实体
客户端,如下
package core.echo.offiDecoder;/*
*
zenglx created at 2017/5/15 11:28
*/
import static java.lang.Thread.sleep;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import java.io.IOException;
import java.util.logging.Logger;
public class EchoPOJOClient {
public void connect() throws InterruptedException, IOException {
String host=&#