客户端如何了解另一个客户端是否已连接 [英] Mqtt How a client can get to know that another client is connected or not

查看:98
本文介绍了客户端如何了解另一个客户端是否已连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我的问题是我需要保持设备(客户端)的状态,因为它们是否在线.

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

我正在通过客户端ID进行连接,通过订阅该主题,我可以知道该设备已断开连接,但是如果它们重新连接,我如何检查它们是否再次连接.

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.

推荐答案

简短的答案是您不同意(在协议级别).

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.

稍长的版本:

您可以对保留的消息和LWT(最后的遗嘱)进行某些操作

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

例如

每个客户端都将保留的消息发布到以下主题

Each client publishes a retained message to the following topic

online/<clientID>

当连接时有效负载为true.如果客户端由于网络故障而断开连接,它还将设置LWT,该LWT将发布false的有效负载.如果客户端完全断开连接,则它需要在下线之前发布false有效负载,这是它的最后一个操作.

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.

要判断客户端是否在线,请订阅online/+的通配符主题,然后检查主题的最后一段以获取clientid和状态的有效负载.

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.

这篇关于客户端如何了解另一个客户端是否已连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆