use of org.bcos.channel.handler.Message in project web3sdk by FISCO-BCOS.
the class Server method onHeartBeat.
public void onHeartBeat(ChannelHandlerContext ctx, Message message) {
String content = "1";
try {
content = new String(message.getData(), "utf-8");
} catch (UnsupportedEncodingException e) {
logger.error("心跳包无法解析");
} catch (Exception e) {
logger.error("心跳包异常");
}
if (content.equals("0")) {
Message response = new Message();
response.setSeq(message.getSeq());
response.setResult(0);
response.setType((short) 0x13);
response.setData("1".getBytes());
ByteBuf out = ctx.alloc().buffer();
response.writeHeader(out);
response.writeExtra(out);
ctx.writeAndFlush(out);
} else if (content.equals("1")) {
}
}
use of org.bcos.channel.handler.Message in project web3sdk by FISCO-BCOS.
the class Server method broadcastTopic.
public void broadcastTopic(ChannelHandlerContext ctx) {
try {
Message message = new Message();
message.setResult(0);
// topic设置topic消息0x32
message.setType((short) 0x32);
message.setSeq(UUID.randomUUID().toString().replaceAll("-", ""));
// 综合所有的topics
Set<String> allTopics = new HashSet<String>();
for (ConnectionInfo connectionInfo : localConnections.getConnections()) {
// 有效的连接,才增加到全局topic
ChannelHandlerContext localCtx = localConnections.getNetworkConnectionByHost(connectionInfo.getHost(), connectionInfo.getPort());
if (localCtx != null && localCtx.channel().isActive()) {
logger.debug("节点:{}:{} 关注topics: {}", connectionInfo.getHost(), connectionInfo.getPort(), connectionInfo.getTopics());
allTopics.addAll(connectionInfo.getTopics());
}
}
message.setData(objectMapper.writeValueAsBytes(allTopics.toArray()));
logger.debug("全部topics: {}", new String(message.getData()));
if (ctx == null) {
// 广播到所有远端节点
for (String key : remoteConnections.getNetworkConnections().keySet()) {
ChannelHandlerContext remoteCtx = remoteConnections.getNetworkConnections().get(key);
if (remoteCtx != null && remoteCtx.channel().isActive()) {
ByteBuf out = remoteCtx.alloc().buffer();
message.writeHeader(out);
message.writeExtra(out);
if (remoteCtx != null && remoteCtx.channel().isActive()) {
logger.debug("topic广播至 {}:{}", ((SocketChannel) remoteCtx.channel()).remoteAddress().getAddress().getHostAddress(), ((SocketChannel) remoteCtx.channel()).remoteAddress().getPort());
remoteCtx.writeAndFlush(out);
}
}
}
} else {
// 发送到指定远端节点
logger.debug("topic发送至 {}:{}", ((SocketChannel) ctx.channel()).remoteAddress().getAddress().getHostAddress(), ((SocketChannel) ctx.channel()).remoteAddress().getPort());
ByteBuf out = ctx.alloc().buffer();
message.writeHeader(out);
message.writeExtra(out);
ctx.writeAndFlush(out);
}
} catch (Exception e) {
logger.error("错误", e);
}
}
Aggregations