MQTT Essential 细节笔记总结(深入理解MQTT细节)

栏目: 后端 · 发布时间: 5年前

如何发现存在的Topic

The subscriber does not know which topics are available. We have seen many deployments where far more than 100.000+ topics are used in the system and sending these topic lists to subscribers would be overkill. The protocol itself does not define such a topic discovery. We have seen these two popular solutions in deployments:

* Subscribe to a custom $SYS topic which contains all available topics for the client (e.g. $SYS/{client_id}/topics). You can learn about hooking in custom $SYS topics

Debug调试的时候的信息的含义

“received PUBLISH (d0, q0, r0, m0, ‘get/123’, … (70 bytes))”

the fields have the following meaning:

d0 => duplicate flag: false

q0 => QoS: 0

r0 => retained: false

m0 => messageID: 0

想要收到有订阅者带走了消息?

MQTT本身无法做到,可以从业务层次实现,比如往另一个Topic里面推送数据

MQTT is a data agnostic protocol that can transfer any kind of information. For that sound files like in your case need to be first converted in byte array before the publish. The subscriber (receiver) needs than to convert back the received byte array.

retain消息不会影响当前已经连接的客户端

也就是只有新连接的客户端才会收到 retain 消息,已经连接的不会收到

One retained message for a specific topic (The last message published on the topic, regardless of the payload) can be stored by the broker so that any new subscribers on that specific topic will be able to get that message. This does not in any way influence the message flow of currently connected subscribers.

如何识别Payload里面的消息类型呢?是否有HTTP那样的Content-Type标志位?

没有任何标志位,可以通过Topic本身的分类来设置不同的消息类型,MQTT是消息无关的协议,不关心自己Payload里面的数据内容

cleanSesson标志位的作用

如果cleanSession=false 那么一旦客户端断开,则对应的Topic

QoS服务降级

只会降级不会升级,也就是会用最低服务质量(发布者发布的消息QoS和订阅者的QoS要求会使用最低质量服务)

如何保证订阅者可以收到所有的消息,即使离线?

All messages will be sent to client B automatically after the reconnect if all of the following conditions are met:

– Client B has been connected before with the clean session flag is false and has subscribe to the topic, where the messages arrive

– All messages were sent with quality of service greater than 0

You can read more on how the queuing works here:  http://www.hivemq.com/blog/mqtt-essentials-part-7-persistent-session-queuing-messages

When looking at retained messages only the last message will be persisted on the topic. It will be delivered to a newly subscribing client, right after subscribing.

如何删除Retain消息?

发送一个含有retain标志位但是payload为空的数据

A zero byte payload message will not be forwarded to subscribers. It’s sole purpose is to clear a retained message on a topic.

如何再次收到retain消息?

断开重连或者取消订阅后重新订阅

The only way to “update” the retained message for a client is to re-subscribe to the topic.

LWT(Last Will Testament) 遗嘱消息什么时候删除?

当客户端Graceful disconnect的时候,它的遗嘱消息就会删除

LWT最佳实践

配合Retain消息使用,比如Retain设置设备在线,这样所有的订阅者都能查询设备状态,但是在LWT中则定义离线,一旦设备离线则broker则会发送离线消息

如果客户端的ID相同(client_id)会怎么样?

client_id用于识别会话,如果相同,这个问题很严重,会导致mqtt认为同一个客户端尝试重新连接,于是会关闭之前的连接然后再新建连接,对于死循环的客户端来说会导致服务器和客户端之间不断的断开和连接,非常消耗资源的死循环流程,因此没有特殊情况,可以不指定clientid,因为大部分库都能够良好的生成随机ID。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

长尾理论

长尾理论

[美]克里斯·安德森 / 中信出版集团股份有限公司 / 2015-8-1 / 59.00元

互联网时代,大众市场不再一统天下,小众市场也可以呼风唤雨。 在《长尾理论》一书中,克里斯·安德森详细阐释了长尾的精华所在,指出商业和文化的未来不在于传统需求曲线上那个代表“畅销商品”的头部,而是那条代表“冷门商品”的经常被人遗忘的长尾。尽管我们仍然对热门商品着迷,但它们对消费者的吸引力已经大不如从前,因为市场已经大大分化。黄金电视节目的收视率几十年来一直在萎缩,若是在七八十年代,现在的一档最......一起来看看 《长尾理论》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具