use of org.fisco.bcos.channel.protocol.parser.HeartBeatParser in project web3sdk by FISCO-BCOS.
the class Service method sendHeartbeatMessage.
public void sendHeartbeatMessage(ChannelHandlerContext ctx) {
Message message = new BcosMessage();
message.setSeq(UUID.randomUUID().toString().replaceAll("-", ""));
message.setResult(0);
message.setType((short) ChannelMessageType.CLIENT_HEARTBEAT.getType());
HeartBeatParser heartBeatParser = new HeartBeatParser(ChannelHandlerContextHelper.getProtocolVersion(ctx));
try {
message.setData(heartBeatParser.encode("0"));
} catch (JsonProcessingException e) {
logger.error(" write json failed, message: {}", e.getMessage());
return;
}
ByteBuf out = ctx.alloc().buffer();
message.writeHeader(out);
message.writeExtra(out);
ctx.writeAndFlush(out);
}
use of org.fisco.bcos.channel.protocol.parser.HeartBeatParser in project web3sdk by FISCO-BCOS.
the class Service method onReceiveHeartbeat.
public void onReceiveHeartbeat(ChannelHandlerContext ctx, Message msg) {
String content = "";
HeartBeatParser heartBeatParser = new HeartBeatParser(ChannelHandlerContextHelper.getProtocolVersion(ctx));
String data = new String(msg.getData());
try {
BcosHeartbeat bcosHeartbeat = heartBeatParser.decode(data);
// logger.trace(" heartbeat packet, heartbeat is {} ", bcosHeartbeat);
int heartBeat = bcosHeartbeat.getHeartBeat();
content = String.valueOf(heartBeat);
} catch (UnsupportedEncodingException e) {
logger.error("heartbeat packet cannot be parsed, data: {}", data);
} catch (Exception e) {
logger.error("heartbeat packet exception, data: {}", data);
}
if ("0".equals(content)) {
logger.trace("heartbeat packet,send heartbeat packet back");
Message response = new Message();
response.setSeq(msg.getSeq());
response.setResult(0);
response.setType((short) ChannelMessageType.CLIENT_HEARTBEAT.getType());
try {
response.setData(heartBeatParser.encode("1"));
} catch (JsonProcessingException e) {
logger.error(" write json failed, message is {} ", e.getMessage());
return;
}
ByteBuf out = ctx.alloc().buffer();
response.writeHeader(out);
response.writeExtra(out);
ctx.writeAndFlush(out);
} else if ("1".equals(content)) {
logger.trace("heartbeat response");
} else {
logger.trace(" unknown heartbeat message , do nothing, data: {}", data);
}
}
Aggregations