@Component
public class MeterNettyServerHandler extends SimpleChannelInboundHandler<Object> {
Mcallback mcallback;
public void addCallback(Mcallback mcallback) {
this.mcallback = mcallback;
}
private static MeterNettyServerHandler nettyServerHandler;
@Autowired
private MeterService meterService;
@PostConstruct
public void init() {
nettyServerHandler = this;
// nettyServerHandler.meterService = this.meterService;
}
/**
* 读客户端数据
*/
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
// 使用ByteBuf收发数据包,不使用编码解码器
ByteBuf buffer = (ByteBuf) msg;
byte[] requestBytes = new byte[buffer.readableBytes()];
buffer.readBytes(requestBytes);// 复制内容到字节数组bytes
String requestStr = HexConvert.BinaryToHexString(requestBytes);
nettyServerHandler.meterService.handleDataPacket(requestBytes, requestStr, ctx);
// handleDataPacket(requestBytes, requestStr, ctx);
ctx.flush();
}
…
…
}
spring 中工具类使用@Autowired
最新推荐文章于 2024-10-11 18:15:00 发布