1、参考之前的文章
netty之服务端热插拔登录校验Handler_非ban必选的博客-CSDN博客
服务端和客户端编解码Handler修改如下
PacketCodecHandler.java ,主要修改解码decode方法
package com.example.demo.im;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageCodec;
import java.util.List;
@ChannelHandler.Sharable
public class PacketCodecHandler extends MessageToMessageCodec<ByteBuf, Packet> {
public static final PacketCodecHandler INSTANCE = new PacketCodecHandler();
private PacketCodecHandler() {
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) {
//解码
//先在此处读取魔数
int magic = byteBuf.readInt();
//若魔数不对 断开连接
i