on_disconnect callback rc = 14
This is because you get a fresh session by default when you reconnect
(because you have clean_session=True), so you have no active
Move the call to client.subscribe('topic') to inside the on_connect
callback then it will resubscribe when it reconnects.
上面虽然说的是paho，但mosquitto其实也是一样的。在mosquitto_new(const char id,bool clean_session,void obj)中，
set to true to instruct the broker to clean all messages and subscriptions on disconnect, false to instruct it to keep them. See the man page mqtt(7) for more details. Note that a client will never discard its own outgoing messages on disconnect. Calling mosquitto_connect or mosquitto_reconnect will cause the messages to be resent. Use mosquitto_reinitialise to reset a client to its original state. Must be set to true if the id parameter is NULL.
a callback function in the following form: void callback(struct mosquitto mosq, void obj, int rc)
mosq the mosquitto instance making the callback.
obj the user data provided in mosquitto_new
rc the return code of the connection response. The values are defined by the MQTT protocol version in use. For MQTT v5.0, look at section 126.96.36.199 Connect Reason code: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html For MQTT v3.1.1, look at section 188.8.131.52 Connect Return code: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html
Subscribe to a topic.
It is valid to use this function for clients using all MQTT protocol versions. If you need to set MQTT v5 SUBSCRIBE properties, use mosquitto_subscribe_v5 instead.
mosq a valid mosquitto instance.
mid a pointer to an int. If not NULL, the function will set this to the message id of this particular message. This can be then used with the subscribe callback to determine when the message has been sent.
sub the subscription pattern.
qos the requested Quality of Service for this subscription.