翻译:https://www.hivemq.com/blog/mqtt-essentials-part-10-alive-client-take-over
心跳机制
Keep Alive指定连接最大空闲时间T,当客户端检测到连接空闲时间超过T时,必须向Broker发送心跳报文PINGREQ,Broker收到心跳请求后返回心跳响应PINGRESP。若Broker超过1.5T时间没收到心跳请求则断开连接,并且投递遗嘱消息到订阅方;同样,若客户端超过一定时间仍没收到心跳响应PINGRESP则断开连接。
连接空闲时发送心跳报文可以降低网络请求,弱化对带宽的依赖。
Keep Alive设定时机
创建连接时,在CONNECT报文中指定,单位s。
Client Take-Over
场景:客户端与Broker连接正常,然后客户端快速重启(小于1.5T),再重新连接Broker,在未达到1.5T这段时间内,客户端与Broker存在两条连接。
处理措施: 先断开之前的连接再建立新的连接。
A disconnected client will most likely try to connect again. It could be the case that the broker still has an half-open connection for the same client. In this scenario the MQTT will perform a so-called client take-over. The broker will close the previous connection to the same client (determined by the same client identifier) and establishes the connection with the newly connected client. This behavior makes sure that half-open connection won’t stand in the way of a new connection establishment of the same client.