为什么一个设备会在很短的时间内订阅又取消订阅又订阅主题

日志如下
2023-11-17T13:26:51.931448+08:00 [MQTT] 1421-666667@122.97.220.86:47192 msg: mqtt_packet_sent, packet: PUBACK(Q0, R0, D0, PacketId=1, ReasonCode=0)
2023-11-17T13:32:31.097627+08:00 [MQTT] 1421-666667@122.97.222.241:19555 msg: mqtt_packet_received, packet: CONNECT(Q0, R0, D0, ClientId=1421-666667, ProtoName=MQTT, ProtoVsn=4, CleanStart=false, KeepAlive=120, Username=abcd, Password=******)
2023-11-17T13:32:31.097877+08:00 [AUTHN] 1421-666667@122.97.222.241:19555 msg: authenticator_result, authenticator: password_based:built_in_database, result: {ok,#{is_superuser => false}}
2023-11-17T13:32:31.097956+08:00 [AUTHN] 1421-666667@122.97.222.241:19555 msg: authentication_result, reason: chain_result, result: {stop,{ok,#{is_superuser => false}}}
2023-11-17T13:32:31.099288+08:00 [SUBSCRIBE] 1421-666667@122.97.222.241:19555 msg: subscribe, sub_id: 1421-666667, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: ], topic: devices/TTC420/1421-666667/battery/set
2023-11-17T13:32:31.099498+08:00 [SUBSCRIBE] 1421-666667@122.97.222.241:19555 msg: subscribe, sub_id: 1421-666667, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: ], topic: devices/TTC420/1421-666667/ota/query
2023-11-17T13:32:31.099611+08:00 [SUBSCRIBE] 1421-666667@122.97.222.241:19555 msg: subscribe, sub_id: 1421-666667, sub_opts: [nl: 0, qos: 2, rap: 0, rh: 0, sub_props: ], topic: devices/TTC420/1421-666667/ota/set
2023-11-17T13:32:31.099732+08:00 [SUBSCRIBE] 1421-666667@122.97.222.241:19555 msg: subscribe, sub_id: 1421-666667, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: ], topic: devices/TTC420/1421-666667/settings/query
2023-11-17T13:32:31.099817+08:00 [SUBSCRIBE] 1421-666667@122.97.222.241:19555 msg: subscribe, sub_id: 1421-666667, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: ], topic: devices/TTC420/1421-666667/settings/set
2023-11-17T13:32:31.099909+08:00 [SUBSCRIBE] 1421-666667@122.97.222.241:19555 msg: subscribe, sub_id: 1421-666667, sub_opts: [nl: 0, qos: 2, rap: 0, rh: 0, sub_props: ], topic: devices/TTC420/all/ota/set
2023-11-17T13:32:31.099990+08:00 [SUBSCRIBE] 1421-666667@122.97.222.241:19555 msg: subscribe, sub_id: 1421-666667, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: ], topic: devices/TTC421/1421-666667/battery/set
2023-11-17T13:32:31.100170+08:00 [SUBSCRIBE] 1421-666667@122.97.222.241:19555 msg: subscribe, sub_id: 1421-666667, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: ], topic: devices/TTC421/1421-666667/ota/query
2023-11-17T13:32:31.100301+08:00 [SUBSCRIBE] 1421-666667@122.97.222.241:19555 msg: subscribe, sub_id: 1421-666667, sub_opts: [nl: 0, qos: 2, rap: 0, rh: 0, sub_props: ], topic: devices/TTC421/1421-666667/ota/set
2023-11-17T13:32:31.100389+08:00 [SUBSCRIBE] 1421-666667@122.97.222.241:19555 msg: subscribe, sub_id: 1421-666667, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: ], topic: devices/TTC421/1421-666667/settings/query
2023-11-17T13:32:31.100477+08:00 [SUBSCRIBE] 1421-666667@122.97.222.241:19555 msg: subscribe, sub_id: 1421-666667, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: ], topic: devices/TTC421/1421-666667/settings/set
2023-11-17T13:32:31.100607+08:00 [SUBSCRIBE] 1421-666667@122.97.222.241:19555 msg: subscribe, sub_id: 1421-666667, sub_opts: [nl: 0, qos: 2, rap: 0, rh: 0, sub_props: ], topic: devices/TTC421/all/ota/set
2023-11-17T13:32:31.100749+08:00 [UNSUBSCRIBE] 1421-666667@122.97.220.86:47192 msg: unsubscribe, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: , subid: 1421-666667], topic: devices/TTC420/1421-666667/battery/set
2023-11-17T13:32:31.100828+08:00 [UNSUBSCRIBE] 1421-666667@122.97.220.86:47192 msg: unsubscribe, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: , subid: 1421-666667], topic: devices/TTC420/1421-666667/ota/query
2023-11-17T13:32:31.100868+08:00 [UNSUBSCRIBE] 1421-666667@122.97.220.86:47192 msg: unsubscribe, sub_opts: [nl: 0, qos: 2, rap: 0, rh: 0, sub_props: , subid: 1421-666667], topic: devices/TTC420/1421-666667/ota/set
2023-11-17T13:32:31.100909+08:00 [UNSUBSCRIBE] 1421-666667@122.97.220.86:47192 msg: unsubscribe, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: , subid: 1421-666667], topic: devices/TTC420/1421-666667/settings/query
2023-11-17T13:32:31.100976+08:00 [UNSUBSCRIBE] 1421-666667@122.97.220.86:47192 msg: unsubscribe, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: , subid: 1421-666667], topic: devices/TTC420/1421-666667/settings/set
2023-11-17T13:32:31.101024+08:00 [UNSUBSCRIBE] 1421-666667@122.97.220.86:47192 msg: unsubscribe, sub_opts: [nl: 0, qos: 2, rap: 0, rh: 0, sub_props: , subid: 1421-666667], topic: devices/TTC420/all/ota/set
2023-11-17T13:32:31.101080+08:00 [UNSUBSCRIBE] 1421-666667@122.97.220.86:47192 msg: unsubscribe, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: , subid: 1421-666667], topic: devices/TTC421/1421-666667/battery/set
2023-11-17T13:32:31.101114+08:00 [UNSUBSCRIBE] 1421-666667@122.97.220.86:47192 msg: unsubscribe, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: , subid: 1421-666667], topic: devices/TTC421/1421-666667/ota/query
2023-11-17T13:32:31.101169+08:00 [UNSUBSCRIBE] 1421-666667@122.97.220.86:47192 msg: unsubscribe, sub_opts: [nl: 0, qos: 2, rap: 0, rh: 0, sub_props: , subid: 1421-666667], topic: devices/TTC421/1421-666667/ota/set
2023-11-17T13:32:31.101228+08:00 [UNSUBSCRIBE] 1421-666667@122.97.220.86:47192 msg: unsubscribe, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: , subid: 1421-666667], topic: devices/TTC421/1421-666667/settings/query
2023-11-17T13:32:31.101267+08:00 [UNSUBSCRIBE] 1421-666667@122.97.220.86:47192 msg: unsubscribe, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: , subid: 1421-666667], topic: devices/TTC421/1421-666667/settings/set
2023-11-17T13:32:31.101302+08:00 [UNSUBSCRIBE] 1421-666667@122.97.220.86:47192 msg: unsubscribe, sub_opts: [nl: 0, qos: 2, rap: 0, rh: 0, sub_props: , subid: 1421-666667], topic: devices/TTC421/all/ota/set
2023-11-17T13:32:31.101965+08:00 [SOCKET] 1421-666667@122.97.220.86:47192 msg: emqx_connection_terminated, reason: {shutdown,takenover}
2023-11-17T13:32:31.103331+08:00 [MQTT] 1421-666667@122.97.222.241:19555 msg: mqtt_packet_sent, packet: CONNACK(Q0, R0, D0, AckFlags=1, ReasonCode=0)
2023-11-17T13:32:31.677487+08:00 [MQTT] 1421-666667@122.97.222.241:19555 msg: mqtt_packet_received, packet: SUBSCRIBE(Q1, R0, D0, PacketId=1 TopicFilters=[devices/TTC421/1421-666667/settings/set(#{nl => 0,qos => 1,rap => 0,rh => 0})])
2023-11-17T13:32:31.678087+08:00 [SUBSCRIBE] 1421-666667@122.97.222.241:19555 msg: subscribe, sub_id: 1421-666667, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: ], topic: devices/TTC421/1421-666667/settings/set
2023-11-17T13:32:31.679210+08:00 [MQTT] 1421-666667@122.97.222.241:19555 msg: mqtt_packet_sent, packet: SUBACK(Q0, R0, D0, PacketId=1, ReasonCodes=[1])
2023-11-17T13:32:32.672945+08:00 [MQTT] 1421-666667@122.97.222.241:19555 msg: mqtt_packet_received, packet: SUBSCRIBE(Q1, R0, D0, PacketId=1 TopicFilters=[devices/TTC421/1421-666667/settings/query(#{nl => 0,qos => 1,rap => 0,rh => 0})])
2023-11-17T13:32:32.673357+08:00 [SUBSCRIBE] 1421-666667@122.97.222.241:19555 msg: subscribe, sub_id: 1421-666667, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: ], topic: devices/TTC421/1421-666667/settings/query
2023-11-17T13:32:32.673474+08:00 [MQTT] 1421-666667@122.97.222.241:19555 msg: mqtt_packet_sent, packet: SUBACK(Q0, R0, D0, PacketId=1, ReasonCodes=[1])
2023-11-17T13:32:33.657703+08:00 [MQTT] 1421-666667@122.97.222.241:19555 msg: mqtt_packet_received, packet: SUBSCRIBE(Q1, R0, D0, PacketId=1 TopicFilters=[devices/TTC421/1421-666667/ota/set(#{nl => 0,qos => 2,rap => 0,rh => 0})])
2023-11-17T13:32:33.657970+08:00 [SUBSCRIBE] 1421-666667@122.97.222.241:19555 msg: subscribe, sub_id: 1421-666667, sub_opts: [nl: 0, qos: 2, rap: 0, rh: 0, sub_props: ], topic: devices/TTC421/1421-666667/ota/set
2023-11-17T13:32:33.658097+08:00 [MQTT] 1421-666667@122.97.222.241:19555 msg: mqtt_packet_sent, packet: SUBACK(Q0, R0, D0, PacketId=1, ReasonCodes=[2])
2023-11-17T13:32:34.696852+08:00 [MQTT] 1421-666667@122.97.222.241:19555 msg: mqtt_packet_received, packet: SUBSCRIBE(Q1, R0, D0, PacketId=1 TopicFilters=[devices/TTC421/all/ota/set(#{nl => 0,qos => 2,rap => 0,rh => 0})])
2023-11-17T13:32:34.697171+08:00 [SUBSCRIBE] 1421-666667@122.97.222.241:19555 msg: subscribe, sub_id: 1421-666667, sub_opts: [nl: 0, qos: 2, rap: 0, rh: 0, sub_props: ], topic: devices/TTC421/all/ota/set
2023-11-17T13:32:34.697271+08:00 [MQTT] 1421-666667@122.97.222.241:19555 msg: mqtt_packet_sent, packet: SUBACK(Q0, R0, D0, PacketId=1, ReasonCodes=[2])
2023-11-17T13:32:35.696811+08:00 [MQTT] 1421-666667@122.97.222.241:19555 msg: mqtt_packet_received, packet: SUBSCRIBE(Q1, R0, D0, PacketId=1 TopicFilters=[devices/TTC421/1421-666667/ota/query(#{nl => 0,qos => 1,rap => 0,rh => 0})])
2023-11-17T13:32:35.697168+08:00 [SUBSCRIBE] 1421-666667@122.97.222.241:19555 msg: subscribe, sub_id: 1421-666667, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: ], topic: devices/TTC421/1421-666667/ota/query
2023-11-17T13:32:35.697305+08:00 [MQTT] 1421-666667@122.97.222.241:19555 msg: mqtt_packet_sent, packet: SUBACK(Q0, R0, D0, PacketId=1, ReasonCodes=[1])
2023-11-17T13:32:36.693850+08:00 [MQTT] 1421-666667@122.97.222.241:19555 msg: mqtt_packet_received, packet: SUBSCRIBE(Q1, R0, D0, PacketId=1 TopicFilters=[devices/TTC421/1421-666667/battery/set(#{nl => 0,qos => 1,rap => 0,rh => 0})])
2023-11-17T13:32:36.694102+08:00 [SUBSCRIBE] 1421-666667@122.97.222.241:19555 msg: subscribe, sub_id: 1421-666667, sub_opts: [nl: 0, qos: 1, rap: 0, rh: 0, sub_props: ], topic: devices/TTC421/1421-666667/battery/set

2023-11-17T13:32:31.101965+08:00 [SOCKET] 1421-666667@122.97.220.86:47192 msg: emqx_connection_terminated, reason: {shutdown,takenover}
2023-11-17T13:32:31.103331+08:00 [MQTT] 1421-666667@122.97.222.241:19555 msg: mqtt_packet_sent, packet: CONNACK(Q0, R0, D0, AckFlags=1, ReasonCode=0)

122.97.220.86:47192122.97.222.241:19555 两个客户端都用了 1421-666667 这个 clientid,他们两个在互相踢。

上面的 SBUSCRIBE 和 UNSUBSCRIBE 也是,122.97.222.241:19555 刚发过 SUB,122.97.220.86:47192 就发了 UNSUB 取消订阅了。

clientid 应该是唯一的。

可是这是同一个客户端,只不过他会定时连接,上报完消息之后会关机,那他关机后再连接,客户端认为他是新的客户端了吗

客户端使用了 cleanstart = false 进行连接,会接管旧 session,拿到之前的订阅关系。

非常感谢,很清楚的解释。因为客户端需要接收在离线期间发布的消息,所以选择了不清除会话。按您的解释,只要会话没有过期,相同clientId 的客户端是不需要重复订阅之前的主题,会自动继承会话里的订阅?

是的,只要之前的会话还存在,没有因为过期被删除/手动删除,新的连接也使用了 cleanstart=false 进行连接,没有清除之前的会话。就可以继承旧会话里的订阅关系。

再咨询下,客户端本身可以得知自己是否连接到上次的会话呢,或者说自己能知道已经继承了上次的订阅吗?

连接返回报文 CONNACK 中,包含来一个 session_present 的一个标志位。你可以在你的 SDK 的链接成功的回调中看看,应该是有这个参数的