libpaho-mqtt c 库使用注意事项

mqtt断线重连:

需要在连接的时候设置三个参数.

重连间隔单位为秒。

 

在例程里

MQTTAsync client;

MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;

MQTTAsync_message pubmsg = MQTTAsync_message_initializer;

MQTTAsync_token token;

int rc;

MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);

MQTTAsync_setCallbacks(client, NULL, connlost, NULL, NULL);

conn_opts.keepAliveInterval = 20;

conn_opts.cleansession = 1;

conn_opts.onSuccess = onConnect;

conn_opts.onFailure = onConnectFailure;

conn_opts.context = client;

if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)

{

printf("Failed to start connect, return code %d\n", rc);

exit(EXIT_FAILURE);

}

conn_opts的设置一定要紧跟在MQTTAsync_connect()之前MQTTAsync_create()之后,否则会在发送或者订阅时产生段错误.

 

你可能感兴趣的:(linux)