Netty 设备在线离线的检测
·
服务端向设备写信息:
Instant currentTs = Instant.ofEpochMilli(System.currentTimeMillis());
String currentTsSTr = currentTs.atZone(ZoneId.systemDefault()).format(formatter);
String tid = "00000000251200000014";
Channel heartChannel = IJT808Cache.SESSIONS.getIfPresent(tid);
if(heartChannel != null) {
String connStatus = heartChannel.isActive() ? "激活" : "失效";
heartChannel.writeAndFlush(Utils.hexToBytes("7E7E")).addListener(future -> {
if(future.isSuccess()) {
logger.info(String.format("%s %s %s, 发送消息成功",currentTsSTr ,tid, connStatus));
} else {
logger.info(String.format("%s %s %s, 发送消息失败",currentTsSTr ,tid, connStatus));
heartChannel.close();
}
});
} else {
logger.info(String.format("%s %s 连接不存在",currentTsSTr ,tid));
}
定时下发消息,会让 IdleStateHandler 处理器失效
pipeline.addLast(new IdleStateHandler(180, 0, 0, TimeUnit.SECONDS));
Netty 读取数据后,要恢复标记位:
byte[] byteArray1 = new byte[decodeData.readableBytes()];
decodeData.readBytes(byteArray1);
decodeData.resetReaderIndex(); // 恢复到标记位
String newMsgText1 = Utils.bytesToHex(byteArray1);
更多推荐



所有评论(0)