我正在使用Paho MQTT Ayncronous版本(libmqttv3a),并且试图找到一种与Mosquitto相似的方式来接收消息, mosquitto具有2个功能 mosquitto_message_callback_set 和 mosquitto_subscribe_callback_set ,第一个功能是接收来自代理的消息,第二个功能是当代理响应订阅请求时。
对于Paho,我只能找到与第二个类似的参数,即MQTTAsync_setCallbacks第四个参数(MQTTAsync_messageArrived回调) 此功能仅处理订阅响应。
我正在搜索类似于mosquitto的东西mosquitto_message_callback_set用于Paho MQTT异步库,有什么想法吗?
Paho文档
这是一个回调函数。客户端应用程序必须提供此功能的实现,以启用消息的异步接收。该函数通过将其作为参数传递给MQTTAsync_setCallbacks()来向客户端库注册。当从服务器接收到与客户端订阅匹配的新消息时,客户端库将调用它。该功能在与运行客户端应用程序的线程不同的线程上执行。
参数 context指向最初传递给MQTTAsync_setCallbacks()的上下文值的指针,该值包含任何特定于应用程序的上下文。
topicName与接收到的消息关联的主题。
topicLen如果topicName中再嵌入一个NULL字符,则主题的长度,否则topicLen为0。如果topicLen为0,则可以信任strlen(topicName)返回的值。如果topicLen大于0,则可以通过访问topicName作为长度为topicLen的字节数组来检索完整的主题名称。
message所接收消息的MQTTAsync_message结构。此结构包含消息有效负载和属性。
返回
此函数必须返回一个布尔值,该值指示客户端应用程序是否已安全接收消息。返回true表示消息已被成功处理。返回false表示存在问题。在这种情况下,客户端库将重新调用MQTTAsync_messageArrived()尝试再次将消息传递到应用程序。
Mosquitto文档
/*
* Function: mosquitto_message_callback_set
*
* Set the message callback. This is called when a message is received from the
* broker.
*
* Parameters:
* mosq - a valid mosquitto instance.
* on_message - a callback function in the following form:
* void callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
*
* Callback Parameters:
* mosq - the mosquitto instance making the callback.
* obj - the user data provided in <mosquitto_new>
* message - the message data. This variable and associated memory will be
* freed by the library after the callback completes. The client
* should make copies of any of the data it requires.
*
* See Also:
* <mosquitto_message_copy>
*/
libmosq_EXPORT void mosquitto_message_callback_set(struct mosquitto *mosq, void (*on_message)(struct mosquitto *, void *, const struct mosquitto_message *));
/*
* Function: mosquitto_subscribe_callback_set
*
* Set the subscribe callback. This is called when the broker responds to a
* subscription request.
*
* Parameters:
* mosq - a valid mosquitto instance.
* on_subscribe - a callback function in the following form:
* void callback(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos)
*
* Callback Parameters:
* mosq - the mosquitto instance making the callback.
* obj - the user data provided in <mosquitto_new>
* mid - the message id of the subscribe message.
* qos_count - the number of granted subscriptions (size of granted_qos).
* granted_qos - an array of integers indicating the granted QoS for each of
* the subscriptions.
*/
libmosq_EXPORT void mosquitto_subscribe_callback_set(struct mosquitto *mosq, void (*on_subscribe)(struct mosquitto *, void *, int, int, const int *));