1234567891011121314151617181920212223242526 |
- package cn.com.zyjblogs.chat.netty;
- import io.netty.buffer.ByteBuf;
- import io.netty.channel.ChannelHandlerContext;
- import io.netty.handler.codec.ReplayingDecoder;
- import java.util.List;
- public class MyMessageDecoder extends ReplayingDecoder<MessageProtocol> {
- @Override
- protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
- System.out.println("\nMyMessageDecoder方法被调用 解码");
- //需要将得到的二进制字节码 转成 MessageProtocol
- int length = in.readInt();
- byte[] content = new byte[length];
- in.readBytes(content);
- //封装成MessageProtocol对象,放入out中,传给下一个handler
- MessageProtocol messageProtocol = new MessageProtocol();
- messageProtocol.setLen(length);
- messageProtocol.setContent(content);
- out.add(messageProtocol);
- }
- }
|