Mqtt How a client can get to know that another cli

2019-01-29 06:55发布

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条回答
欢心
2楼-- · 2019-01-29 07:54

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.

查看更多
登录 后发表回答