当前位置:   article > 正文

paho.mqtt.c 库的使用(异步客户端)_mqttclient_responseoptions

mqttclient_responseoptions
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <stdbool.h>
  5. #include "MQTTAsync.h"
  6. MQTTAsync mqttClient;
  7. static const char *subTopic = "xxx"; //设备订阅的主题
  8. static const char *pubTopic = "xxx"; //设备发布的主题
  9. int sendMessage(char *data, int qos) //发送数据
  10. {
  11. int rc;
  12. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  13. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  14. opts.context = mqttClient;
  15. //opts.onSuccess = NULL; /*发送成功回调*/
  16. //opts.onFailure = NULL; /*发送失败回调*/
  17. pubmsg.payload = data;
  18. pubmsg.payloadlen = strlen(data);
  19. pubmsg.qos = qos;
  20. printf("mqtt send: %s\n", (char *)pubmsg.payload);
  21. //尝试发送数据,想要知道消息是否发送成功,需要设置 opts 中的相关回调
  22. if ((rc = MQTTAsync_sendMessage(mqttClient, pubTopic, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
  23. {
  24. printf("MQTTAsync_sendMessage() fail, error code:%d\n", rc);
  25. }
  26. return 0;
  27. }
  28. void onConnectCallCBack(void *context, char *cause) //连接成功回调
  29. {
  30. printf("Connection successful.\n");
  31. int rc, qos;
  32. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  33. qos = 0;
  34. opts.context = mqttClient;
  35. //opts.onSuccess = NULL; /*订阅成功回调*/
  36. //opts.onFailure = NULL; /*订阅失败回调*/
  37. if ((rc = MQTTAsync_subscribe(mqttClient, subTopic, qos, &opts)) != MQTTASYNC_SUCCESS) //尝试订阅主题
  38. {
  39. printf("MQTTAsync_subscribe() fail, error code:%d\n", rc);
  40. }
  41. }
  42. void onConnectFailure(void *context, MQTTAsync_failureData *response) //连接失败回调
  43. {
  44. printf("connection fail, error code: %d, error message:%s\n", response ? response->code : 0, response ? response->message : 0);
  45. }
  46. void connlost(void *context, char *cause) //连接断开回调
  47. {
  48. printf("disconnect, cause: %s\n", cause);
  49. }
  50. int msgarrvd(void *context, char *pubTopic, int topicLen, MQTTAsync_message *message) //接收数据回调
  51. {
  52. char buf[message->payloadlen + 1] = {0};
  53. memcpy(buf, message->payload, message->payloadlen);
  54. printf("mqtt recv: %s\n", buf);
  55. MQTTAsync_freeMessage(&message);
  56. MQTTAsync_free(pubTopic);
  57. return 1;
  58. }
  59. void mqtt()
  60. {
  61. int rc;
  62. const char *serverUrl = "xxx"; //服务器地址
  63. const char *clientId = "xxx"; //客户端标识符
  64. const char *userName = "xxx"; //用户名
  65. const char *password = "xxx"; //密码
  66. MQTTAsync_create(&mqttClient, serverUrl, clientId, MQTTCLIENT_PERSISTENCE_NONE, NULL); //创建mqtt客户端
  67. MQTTAsync_setCallbacks(mqttClient, NULL, connlost, msgarrvd, NULL); //设置连接断开和接受数据回调
  68. MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer; //初始化结构体
  69. conn_opts.cleansession = 1;
  70. conn_opts.username = userName;
  71. conn_opts.password = password;
  72. //conn_opts.onSuccess = onConnect; /*连接成功回调,此处设置的回调只在第一次连接成功时调用,SDK内部自动重连时不会调用;*/
  73. conn_opts.onFailure = onConnectFailure; //连接失败回调
  74. conn_opts.context = mqttClient;
  75. conn_opts.automaticReconnect = true; //开启断开自动重连
  76. conn_opts.minRetryInterval = 3; //最小重连间隔时间(秒),每次失败重连间隔时间都会加倍
  77. conn_opts.maxRetryInterval = 60; //最大重连间隔时间(秒)
  78. MQTTAsync_setConnected(mqttClient, NULL, onConnectCallCBack); //设置连接成功回调,不管是第一次连接成功还是重连成功都会调用此回调
  79. if ((rc = MQTTAsync_connect(mqttClient, &conn_opts)) != MQTTASYNC_SUCCESS) //尝试连接
  80. {
  81. printf("MQTTAsync_connect() fail, error code:%d\n", rc);
  82. }
  83. }

关于各回调函数的详细介绍:

PahoMQTT-c:异步模式下各回调函数的使用场景 - 知乎 (zhihu.com)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/176355
推荐阅读
相关标签
  

闽ICP备14008679号