Mqtt How a client can get to know that another client is connected or not

ぃ、小莉子 提交于 2019-12-18 09:42:21

问题


Hello guys my problem is that I need to keep device(clients) status as they are online or not.

I am connecting through a client id and from subscribing there will topic I can get to know that device is disconnected but if they connect again how can I check they connect again.


回答1:


The short answer is you don't (at a protocol level).

Publishers and subscribers are totally unaware of each other, messages are sent to topics not to specific subscribing clients.

The slightly longer version:

You could do something with retained messages and LWT (Last Will & Testament)

e.g.

Each client publishes a retained message to the following topic

online/<clientID>

With a payload of true when it connects. It also sets up LWT that will publish a payload of false if the client is disconnected by a network fault. If the client cleanly disconnects then it will need to publish the false payload as it's last action before going offline.

To tell if client is online you subscribe to the wildcard topic of online/+ and then check the last segment of the topic for clientid and the payload for the state.



来源:https://stackoverflow.com/questions/38280283/mqtt-how-a-client-can-get-to-know-that-another-client-is-connected-or-not

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!