在服务器中使用proto UDP发送POJO可序列化,但在客户端如何使用netty反序列化UDP DatagramPacket?
答案 0 :(得分:1)
只需扩展此处理程序
abstract public class BaseChannelInboundHandler<D> extends SimpleChannelInboundHandler<DatagramPacket> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket datagramPacket) throws Exception {
try {
ByteBuf buf = datagramPacket.content();
byte[] bytes = new byte[buf.readableBytes()];
buf.readBytes(bytes);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
ObjectInputStream inputStream = new ObjectInputStream(byteArrayInputStream);
Object obj = inputStream.readObject();
@SuppressWarnings("unchecked")
D d = (D) obj;
messageReceived(ctx, d);
} catch (Exception e) {
FLog.e(e);
}
}
abstract protected void messageReceived(ChannelHandlerContext ctx, D data);
}