用到了netty的UDP协议,获取到客户端传来的封包,通过content() 方法获取到客户端发来的消息内容 ,获取到的是ByteBuf 类型的,就用到了BytBuf转字节流,代码如下:
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket datagramPacket) throws Exception {
ByteBuf byteBuf = datagramPacket.content();
CCloudPacketParser cCloudPacketParser = new CCloudPacketParser();
InputStream io = new ByteBufInputStream(byteBuf);
CPacket12345 packet12345 = cCloudPacketParser.parse(io);
System.out.println("获取到的数据:" + packet12345.getMsgType().getElevator_id());
System.out.println("获取到的数据:" + packet12345.getMsgType().getMsg_type());
}
使用netty的ByteBufInputStream() 即可得到想要的类型
|