当前位置:   article > 正文

Paho-MQTT C 库使用_mqtt will mqttasync_willoptions

mqtt will mqttasync_willoptions

1、MQTTAsync_connectOptions

  1. typedef struct
  2. {
  3. /** The eyecatcher for this structure. must be MQTC. */
  4. char struct_id[4];
  5. /** The version number of this structure. Must be 0, 1, 2, 3 4 5 6, 7 or 8.
  6. * 0 signifies no SSL options and no serverURIs
  7. * 1 signifies no serverURIs
  8. * 2 signifies no MQTTVersion
  9. * 3 signifies no automatic reconnect options
  10. * 4 signifies no binary password option (just string)
  11. * 5 signifies no MQTTV5 properties
  12. * 6 signifies no HTTP headers option
  13. * 7 signifies no HTTP proxy and HTTPS proxy options
  14. */
  15. int struct_version;
  16. /** The "keep alive" interval, measured in seconds, defines the maximum time
  17. * that should pass without communication between the client and the server
  18. * The client will ensure that at least one message travels across the
  19. * network within each keep alive period. In the absence of a data-related
  20. * message during the time period, the client sends a very small MQTT
  21. * "ping" message, which the server will acknowledge. The keep alive
  22. * interval enables the client to detect when the server is no longer
  23. * available without having to wait for the long TCP/IP timeout.
  24. * Set to 0 if you do not want any keep alive processing.
  25. */
  26. int keepAliveInterval;
  27. /**
  28. * This is a boolean value. The cleansession setting controls the behaviour
  29. * of both the client and the server at connection and disconnection time.
  30. * The client and server both maintain session state information. This
  31. * information is used to ensure "at least once" and "exactly once"
  32. * delivery, and "exactly once" receipt of messages. Session state also
  33. * includes subscriptions created by an MQTT client. You can choose to
  34. * maintain or discard state information between sessions.
  35. *
  36. * When cleansession is true, the state information is discarded at
  37. * connect and disconnect. Setting cleansession to false keeps the state
  38. * information. When you connect an MQTT client application with
  39. * MQTTAsync_connect(), the client identifies the connection using the
  40. * client identifier and the address of the server. The server checks
  41. * whether session information for this client
  42. * has been saved from a previous connection to the server. If a previous
  43. * session still exists, and cleansession=true, then the previous session
  44. * information at the client and server is cleared. If cleansession=false,
  45. * the previous session is resumed. If no previous session exists, a new
  46. * session is started.
  47. */
  48. int cleansession;
  49. /**
  50. * This controls how many messages can be in-flight simultaneously.
  51. */
  52. int maxInflight;
  53. /**
  54. * This is a pointer to an MQTTAsync_willOptions structure. If your
  55. * application does not make use of the Last Will and Testament feature,
  56. * set this pointer to NULL.
  57. */
  58. MQTTAsync_willOptions* will;
  59. /**
  60. * MQTT servers that support the MQTT v3.1 protocol provide authentication
  61. * and authorisation by user name and password. This is the user name
  62. * parameter.
  63. */
  64. const char* username;
  65. /**
  66. * MQTT servers that support the MQTT v3.1 protocol provide authentication
  67. * and authorisation by user name and password. This is the password
  68. * parameter.
  69. */
  70. const char* password;
  71. /**
  72. * The time interval in seconds to allow a connect to complete.
  73. */
  74. int connectTimeout;
  75. /**
  76. * The time interval in seconds after which unacknowledged publish requests are
  77. * retried during a TCP session. With MQTT 3.1.1 and later, retries are
  78. * not required except on reconnect. 0 turns off in-session retries, and is the
  79. * recommended setting. Adding retries to an already overloaded network only
  80. * exacerbates the problem.
  81. */
  82. int retryInterval;
  83. /**
  84. * This is a pointer to an MQTTAsync_SSLOptions structure. If your
  85. * application does not make use of SSL, set this pointer to NULL.
  86. */
  87. MQTTAsync_SSLOptions* ssl;
  88. /**
  89. * A pointer to a callback function to be called if the connect successfully
  90. * completes. Can be set to NULL, in which case no indication of successful
  91. * completion will be received.
  92. */
  93. MQTTAsync_onSuccess* onSuccess;
  94. /**
  95. * A pointer to a callback function to be called if the connect fails.
  96. * Can be set to NULL, in which case no indication of unsuccessful
  97. * completion will be received.
  98. */
  99. MQTTAsync_onFailure* onFailure;
  100. /**
  101. * A pointer to any application-specific context. The
  102. * the <i>context</i> pointer is passed to success or failure callback functions to
  103. * provide access to the context information in the callback.
  104. */
  105. void* context;
  106. /**
  107. * The number of entries in the serverURIs array.
  108. */
  109. int serverURIcount;
  110. /**
  111. * An array of null-terminated strings specifying the servers to
  112. * which the client will connect. Each string takes the form <i>protocol://host:port</i>.
  113. * <i>protocol</i> must be <i>tcp</i>, <i>ssl</i>, <i>ws</i> or <i>wss</i>.
  114. * The TLS enabled prefixes (ssl, wss) are only valid if a TLS version of the library
  115. * is linked with.
  116. * For <i>host</i>, you can
  117. * specify either an IP address or a domain name. For instance, to connect to
  118. * a server running on the local machines with the default MQTT port, specify
  119. * <i>tcp://localhost:1883</i>.
  120. */
  121. char* const* serverURIs;
  122. /**
  123. * Sets the version of MQTT to be used on the connect.
  124. * MQTTVERSION_DEFAULT (0) = default: start with 3.1.1, and if that fails, fall back to 3.1
  125. * MQTTVERSION_3_1 (3) = only try version 3.1
  126. * MQTTVERSION_3_1_1 (4) = only try version 3.1.1
  127. */
  128. int MQTTVersion;
  129. /**
  130. * Reconnect automatically in the case of a connection being lost?
  131. */
  132. int automaticReconnect;
  133. /**
  134. * Minimum retry interval in seconds. Doubled on each failed retry.
  135. */
  136. int minRetryInterval;
  137. /**
  138. * Maximum retry interval in seconds. The doubling stops here on failed retries.
  139. */
  140. int maxRetryInterval;
  141. /**
  142. * Optional binary password. Only checked and used if the password option is NULL
  143. */
  144. struct {
  145. int len; /**< binary password length */
  146. const void* data; /**< binary password data */
  147. } binarypwd;
  148. /*
  149. * MQTT V5 clean start flag. Only clears state at the beginning of the session.
  150. */
  151. int cleanstart;
  152. /**
  153. * MQTT V5 properties for connect
  154. */
  155. MQTTProperties *connectProperties;
  156. /**
  157. * MQTT V5 properties for the will message in the connect
  158. */
  159. MQTTProperties *willProperties;
  160. /**
  161. * A pointer to a callback function to be called if the connect successfully
  162. * completes. Can be set to NULL, in which case no indication of successful
  163. * completion will be received.
  164. */
  165. MQTTAsync_onSuccess5* onSuccess5;
  166. /**
  167. * A pointer to a callback function to be called if the connect fails.
  168. * Can be set to NULL, in which case no indication of unsuccessful
  169. * completion will be received.
  170. */
  171. MQTTAsync_onFailure5* onFailure5;
  172. /**
  173. * HTTP headers for websockets
  174. */
  175. const MQTTAsync_nameValue* httpHeaders;
  176. /**
  177. * HTTP proxy
  178. */
  179. const char* httpProxy;
  180. /**
  181. * HTTPS proxy
  182. */
  183. const char* httpsProxy;
  184. } MQTTAsync_connectOptions;

keepAliveInterval

LinkedIn Login, Sign in | LinkedIn

MQTT 协议 Keep Alive 详解 | EMQ

并且由于我们的broker在internet上,broker和客户端的通讯必须要加密。所以这篇文章介绍的Mosquitto安装会包含使用用户名/密码的身份验证和SSL数据传输加密机制。对于SSL所需要的证书的申请和配置,本文介绍了两种方法。一种通过Let’s Encrypt申请免费证书。一种是采用自签名的证书。如果客户端不需要采用证书认证的方式,就推荐用Let’s Encrypt去生成服务器证书,因为Let’s Encrypt是一个被广泛认可的trusted CA,其发布的证书安全性更高。

虽然使用了用户名加密码的方式可以用来验证发布或订阅消息客户端的合法性,但用户名和密码在internet上是明文传输的,属于裸奔行为,非常容易被第三方截取。下面我们将使用SSL的方式对通讯进行加密。

  1. /etc/mosquitto/certs/ca.crt:服务器自签名CA证书
  2. /etc/mosquitto/certs/client.crt:客户端证书
  3. /etc/mosquitto/certs/client.key:客户端密钥文件
  4. #define BRIXBOT_ADDRESS "ssl://www.domain.com:8883" // You have to change it to your broker
  5. #define BRIXBOT_USER "bot" // You have to change it to a valid user name of your broker
  6. #define BRIXBOT_PASSWORD "password" // You have to change it to the user's password
  7. #define BRIXBOT_SERVER_CA_FILE "../../ssl/ca.crt"
  8. #define BRIXBOT_CLIENT_CRT_FILE "../../ssl/client.crt"
  9. #define BRIXBOT_CLIENT_KEY_FILE "../../ssl/client.key"
  10. MQTTAsync_SSLOptions sslOpts = MQTTAsync_SSLOptions_initializer;
  11. sslOpts.trustStore = BRIXBOT_SERVER_CA_FILE;
  12. sslOpts.keyStore = BRIXBOT_CLIENT_CRT_FILE;
  13. sslOpts.privateKey = BRIXBOT_CLIENT_KEY_FILE;
  14. sslOpts.verify = 1;
  15. ...
  16. connOpts.ssl = &sslOpts;
  • BRIXBOT_ADDRESS:前面的ssl表明我要用SSL加密去和broker通讯。后面是broker的域名和SSL通讯端口
  • BRIXBOT_USERBRIXBOT_PASSWORD:是用来以用户名密码的方式进行身份验证。其值要和broker创建的用户一致。请参考《MQTT什么鬼?第二讲:安装MQTT服务器(broker)
  • BRIXBOT_SERVER_CA_FILE:是服务器自签名CA证书,用于SSL验证
  • BRIXBOT_CLIENT_CRT_FILEBRIXBOT_CLIENT_KEY_FILE:分别为服务器给客户端生成的客户端证书和密钥文件。请参考《MQTT什么鬼?第三讲:史上最安全的MQTT服务

2、MQTTAsync_createOptions 

◆ sendWhileDisconnected

Whether to allow messages to be sent when the client library is not connected.

This feature was not originally available because with persistence enabled, messages could be stored locally without ever knowing if they could be sent. The client application could have created the client with an erroneous broker address or port for instance.

To enable messages to be published when the application is disconnected MQTTAsync_createWithOptions must be used instead of MQTTAsync_create to create the client object. The MQTTAsync_createOptions field sendWhileDisconnected must be set to non-zero, and the maxBufferedMessages field set as required - the default being 100.

MQTTAsync_getPendingTokens can be called to return the ids of the messages waiting to be sent, or for which the sending process has not completed.

◆ maxBufferedMessages

The maximum number of messages allowed to be buffered. This is intended to be used to limit the number of messages queued while the client is not connected. It also applies when the client is connected, however, so has to be greater than 0.

Pending tokens, inflight messages and buffered messages · Issue #1094 · eclipse/paho.mqtt.c · GitHub

Setting maxBufferedMessages to zero always gets a publish error · Issue #1193 · eclipse/paho.mqtt.c · GitHub

Paho Asynchronous MQTT C Client Library: Publish While Disconnected

Question about persistence and how configure it properly · Issue #1092 · eclipse/paho.mqtt.c · GitHub

ref:

Coding – 砖瓦匠

Paho-MQTT C 库使用 – 人人都懂物联网

Paho Asynchronous MQTT C Client Library: MQTTAsync_createOptions Struct Reference
GitHub - eclipse/paho.mqtt.c: An Eclipse Paho C client library for MQTT for Windows, Linux and MacOS. API documentation: https://eclipse.github.io/paho.mqtt.c/

MQTT----client paho.mqtt.c交叉编译_Stay Hungry Stay Foolish-CSDN博客_paho.mqtt.c编译

 https://github.com/google/boringssl

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/176398
推荐阅读
相关标签
  

闽ICP备14008679号