问题描述
我肯定缺少有关整个MQTT协议如何工作的信息,因为我无法掌握最后遗嘱消息的用法模式:它们的目的是什么?
我经常看到的一个例子是有关通知设备已脱机.对我而言,这没有多大意义,因为很明显,如果设备未发布任何数据,则它可能处于脱机状态,或者可能存在一些网络问题.
I'm surely missing something about how the whole MQTT protocol works, as I can't grasp the usage pattern of Last Will Testament messages: what's their purpose?
One example I often see is about informing that a device has gone offline. It doesn't make very much sense to me, since it's obvious that if a device isn't publishing any data it may be offline or there could be some network problems.
那么,LWT的一些实际用法是什么?它是干什么的?
So, what are some practical usages of the LWT? What was it invented for?
推荐答案
LWT消息并不真正关心检测客户端是否已脱机(该任务由keepAlive消息处理).LWT消息与客户端脱机后发生的情况有关.
LWT messages are not really concerned about detecting whether a client has gone offline or not (that task is handled by keepAlive messages). LWT messages are about what happens after the client has gone offline.
类比是 real 的最后一个例子:如果一个人去世,她可以制定遗嘱,在该遗嘱中声明去世后应采取的行动.遗嘱执行人会留意那些愿望,并代她执行.MQTT世界中的一个类比是,客户可以制定一个遗嘱,在该遗嘱中,它声明脱机后代理应该代表它发送什么消息.
The analogy is that of a real last will: If a person dies, she can formulate a testament, in which she declares what actions should be taken after she has passed away. An executor will heed those wishes and execute them on her behalf. The analogy in the MQTT world is that a client can formulate a testament, in which it declares what message should be sent on it's behalf by the broker, after it has gone offline.
一个虚构的示例:
我有一个传感器,它可以发送关键数据,但是很少发送.它以[topic:'/node/gone-offline',message:':id']的形式制定了最后的遗嘱声明,其中:id是传感器的唯一ID.我还为主题"node/gone-offline"提供了 emergency-subscriber ,每次在该频道上发布消息时,该消息都会向我的手机发送一条SMS.
I have a sensor, which sends crucial data, but very infrequently. It has formulated a last will statement in the form of [topic: '/node/gone-offline', message: ':id'], with :id being a unique id for the sensor. I also have a emergency-subscriber for the topic 'node/gone-offline', which will send a SMS to my phone every time a message is published on that channel.
在正常操作期间,传感器将通过发送定期的keepAlive消息,并在传感器的实际读数之间进行穿插,以保持与MQTT-经纪人的连接打开.如果传感器脱机,则由于缺少keepAlives,与代理的连接将超时.
During normal operation, the sensor will keep the connection to the MQTT-broker open by sending periodic keepAlive messages interspersed with the actual sensor readings. If the sensor goes offline, the connection to the broker will time out, due to the lack of keepAlives.
这是LWT的来历:如果未指定LWT,则代理将不在乎,仅关闭连接.但是,在我们的情况下,代理将执行传感器的最后遗嘱并发布LWT消息"/node/gone-offline::id".然后,该消息将被消耗给我的紧急订阅者,并且将通过SMS通知我传感器的ID,以便我可以检查发生了什么情况.
This is where LWT comes in: If no LWT is specified, the broker doesn't care and just closes the connection. In our case however, the broker will execute the sensor's last will and publish the LWT-message '/node/gone-offline: :id'. The message will then be consumed to my emergency-subscriber and I will be notified of the sensor's ID via SMS so that I can check up on what's going on.
简而言之:
LWT消息不仅可以在客户端脱机后仅关闭连接,还可以利用LWT消息来定义要由代理代表客户端发布的消息,因为客户端处于脱机状态并且无法再发布.
Instead of just closing the connection after a client has gone offline, LWT messages can be leveraged to define a message to be published by the broker on behalf of the client, since the client is offline and cannot publish anymore.
这篇关于MQTT《最后遗嘱》的目的或用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!