MQTT 心跳机制

翻译: https://www.hivemq.com/blog/mqtt-essentials-part-10-alive-client-take-over/

 

If the client does not send a messages during the keep-alive period, it must send a PINGREQ packet to the broker to confirm that it is available and to make sure that the broker is also still available.

如果客户端在保持活动期间未发送消息,则必须向代理发送PINGREQ包,以确认它是否可用,并确保代理仍然可用。

The broker must disconnect a client that does not send a message or a PINGREQ packet in one and a half times the keep alive interval. Likewise, the client is expected to close the connection if it does not receive a response from the broker in a reasonable amount of time.

代理必须断开客户端,该客户端在保持活动间隔的1.5T 内不发送消息或PINGREQ包。同样,如果客户机在合理的时间内没有收到代理的响应,则预期它将关闭连接。

说明:自己开发的体会,MQTT是基于TCP实现的,TCP里面的连接关闭,需要等待1.5个TLS,所以在开发MQTT时设置的是1.5TLS时间间隔,1.5TLS没有收到PINGREQ,认为连接断开,可以终止MQTT连接。

Keep Alive Flow

Let’s take a closer look at the keep alive messages. The keep alive feature uses two packets:

PINGREQ

pingreq

固定报头: 报头控制类型(0xc0),报文PINGREQ没有可变报头。

有效载荷: 剩余长度等于0, 报文PINGREQ没有有效载荷。

响应:  server收到PINGREQ报文时,必须使用PINGRESP报文响应。

The PINGREQ is sent by the client and indicates to the broker that the client is still alive. If the client does not send any other type of packets (for example, a PUBLISH or SUBSCRIBE packet), the client must send a PINGREQ packet to the broker. The client can send a PINGREQ packet any time it wants to confirm that the network connection is still alive. The PINGREQ packet does not contain a payload.

PINGREQ由客户端发送,并向代理指示客户端仍然有效。如果客户机不发送任何其他类型的数据包(例如,发布或订阅数据包),则客户机必须向代理发送PINGREQ数据包。客户机可以在任何时候发送一个PINGREQ数据包来确认网络连接仍然有效。PINGREQ数据包不包含有效负载。

 

PINGRESP

pingresp

server发送给client的PINGRESP,用于确认它还活着。保持连接(Keep Alive)处理中用到这个报文。

固定报头: 报文PINGRESP没有可变报头。

有效载荷: 报文PINGRESP没有有效载荷。

When the broker receives a PINGREQ packet, the broker must reply with a PINGRESP packet to show the client that it is still available. The PINGRESP packet also does not contain a payload.

当代理收到PINGREQ包时,代理必须用PINGRESP包进行应答,以向客户机显示它仍然可用。PINGRESP包也不包含有效负载。

Good to Know

  • If the broker does not receive a PINGREQ or any other packet from a client, the broker closes the connection and sends the last will and testament message (if the client specified an LWT).   如果代理没有从客户端接收到PINGREQ或任何其他数据包,那么代理将关闭连接并发送最后一条will和testament消息(如果客户端指定了LWT)。
  • It is the responsibility of the MQTT client to set an appropriate keep alive value. For example, the client can adjust the keep-alive interval to its current signal strength.     MQTT客户机负责设置适当的keep-alive值。例如,客户机可以将keep alive间隔调整为其当前信号强度。
  • The maximum keep alive is 18h 12min 15 sec.      最大存活时间为18小时12分15秒。
  • If the keep alive interval is 0, the keep alive mechanism is deactivated.      如果保持活动间隔为0,则停用保持活动机制。
### MQTT 心跳机制实现保持连接 alive 在 MQTT 协议中,心跳机制的核心目标是通过定期交换特定的控制报文来维持客户端和服务端之间的连接状态,并及时发现异常断连情况。以下是关于该机制的关键点: #### 1. **心跳间隔定义** 客户端在与服务端建立连接时,会在 `CONNECT` 报文中指定一个名为 `Keep Alive` 的时间间隔(单位为秒),表示客户端承诺每隔不超过此时间段就向服务端发送一次通信数据[^2]。 #### 2. **心跳请求 (PINGREQ)** 和 响应 (PINGRESP) 如果在一个完整的 `Keep Alive` 时间周期内没有任何其他类型的 MQTT 控制包传输,则客户端需主动发出 PINGREQ 报文作为心跳信号;而服务端接收到后会立即返回对应的 PINGRESP 报文予以确认[^3]。 #### 3. **超时判定逻辑** - 对于服务端来说,如果超过 1.5倍设定好的 `Keep Alive` 时间仍未收到来自某客户端的有效通讯(包括但不限于应用层消息或者上述提到的心跳指令),则可认定此次链接失效并采取相应措施结束会话。 - 类似地,当客户端长时间未得到预期回应也可能自行判断网络状况不佳进而尝试重新握手恢复链路正常运作[^5]。 #### 4. **实际应用场景中的注意事项** 开发者应当合理配置合适的 keep-alive 参数值以平衡资源消耗同可靠性需求之间关系——太短可能导致频繁不必要的交互增加带宽占用率;反之过长又可能延迟故障感知速度影响用户体验效果[^4]。 ```python import paho.mqtt.client as mqtt def on_connect(client, userdata, flags, rc): print(f"Connected with result code {rc}") client = mqtt.Client() client.on_connect = on_connect client.connect("mqtt.example.com", 1883, 60) # 设置keepalive时间为60s client.loop_forever() ``` 上面展示了一个简单的 Python 脚本片段利用 PAHO 库实现了基本功能的同时设置了合理的 KeepAlive 参数实例演示如何操作设置过程[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值