从MQTT客户端获取连接的客户端ID的列表 [英] Get a list of connected client IDs from MQTT client
问题描述
作为连接到mosquitto的mqtt客户端,是否可以检索也连接到代理的客户端ID列表?
As a mqtt client connected to mosquitto is it possible to retrieve a list of client IDs who are also connected to the broker?
推荐答案
方法I:客户端逻辑中的句柄
如@ user1048839所说,请使用客户端的LWT
&在线发布味精,
在自定义主题上维护客户状态.
下标该主题维护客户列表自身.
method I: handle in client logic
as @user1048839 says, use client's LWT
& online publish msg,
maintain client status on a custom topic.
subscript this topic & maintain client list self.
如果发布retain
消息,则sub将获得客户端列表.
if pub retain
msg, once sub will get the client list.
官方代码不支持在线列表,
所以我修补了mosquitto 1.5.4,添加了2个自定义sys主题:
official code not support online_list,
so I patched mosquitto 1.5.4, add 2 custom sys topic:
mosquitto_sub -i DDD -v -t '$SYS/broker/chen_list'
$SYS/broker/chen_list
0 - CLOUD0_19108
1 - EEE
2 - DDD
2.线上/线下活动
mosquitto_sub -i DDD -v -t '$SYS/broker/chen_state/#'
$SYS/broker/chen_state/DDD 1
$SYS/broker/chen_state/EEE 1
$SYS/broker/chen_state/CLOUD0_19108 1
$SYS/broker/chen_state/EEE 0
$SYS/broker/chen_state/EEE 1
//如果pub retain
msg,则本主题下的子主题可以使所有客户端处于联机状态(在有效负载中).
// if pub retain
msg, sub this topic can get all clients online state (in payload).
在github上测试源代码:
test source code on github:
这篇关于从MQTT客户端获取连接的客户端ID的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!