MQTTAsync_onSuccess和MQTTAsync_deliveryComplete回调之间有什么区别? [英] What is difference between MQTTAsync_onSuccess and MQTTAsync_deliveryComplete callbacks?

查看:1447
本文介绍了MQTTAsync_onSuccess和MQTTAsync_deliveryComplete回调之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过阅读和尝试异步

I'm learning about MQTT (specifically the paho C library) by reading and experimenting with variations on the async pub/sub examples.

aliverlety 您通过 MQTTAsync_set设置的回调>与 MQcessAsyncon>或href ="https://www.eclipse.org/paho/files/mqttdoc/MQTTAsync/html/_m_q_t_t_t_t_async_8h.html#a892cf122e6e8d8f6cd38c4c8efe8fb67" rel ="nofollow noreferrer">您设置的回调MQTTAsync_onSuccess5 MQTTAsync_responseOptions 结构您将传递到 MQTTAsync_aendMessage()

What's the difference between the MQTTAsync_deliveryComplete callback that you set with MQTTAsync_setCallbacks() vs. the MQTTAsync_onSuccess or MQTTAsync_onSuccess5 callbacks that you set in the MQTTAsync_responseOptions struct that you pass to MQTTAsync_sendMessage() ?

所有人似乎都在处理已发布消息的成功传递",但是通过阅读示例代码和doxygen,我无法辨别它们是如何相互关联或冲突或相辅相成的.感谢任何指导.

All seem to deal with "successful delivery" of published messages, but from reading the example code and doxygen, I can't tell how they relate to or conflict with or supplement each other. Grateful for any guidance.

推荐答案

MQTTAsync_deliveryComplete和MQTTAsync_onSuccess基本上相同,它们通过回调通知您消息的传递.这两个回调都是在与运行客户端应用程序的线程不同的线程上异步执行的.

Basically MQTTAsync_deliveryComplete and MQTTAsync_onSuccess do the same, they notify you via callback about the delivery of a message. Both callbacks are executed asynchronously on a separate thread to the thread on which the client application is running.

(在当前版本的Paho客户端中,两个回调甚至都使用相同的线程,但这是未记录的实现细节.MQTTAsync_deliveryComplete和MQTTAsync_onSuccess使用的线程当然不是应用程序线程,否则不会是异步回调).

(Both callbacks are even using the same thread in the case of the current version of the Paho client, but this is a non-documented implementation detail. This thread used by MQTTAsync_deliveryComplete and MQTTAsync_onSuccess is of course not the application thread otherwise it would not be an asynchronous callback).

区别在于,通过MQTTAsync_setCallbacks在一次上设置了MQTTAsync_deliveryComplete回调,然后通知您条消息的传递.

The difference is that MQTTAsync_deliveryComplete callback is set once via MQTTAsync_setCallbacks and then you are informed about every delivery of a message.

与此相反,MQTTAsync_onSuccess会一次通知您您通过MQTTAsync_sendMessage()发送的消息.

In contrast to this, the MQTTAsync_onSuccess informs you once for exactly the message that you send out via MQTTAsync_sendMessage().

您甚至可以定义两个回调,这两个回调都会在传递消息时被同时调用.

You can even define both callbacks, which will both be called when a message is delivered.

这使您可以灵活地选择最适合您的需求的方法.

This gives you the flexibility to choose the approach that best suits your needs.

人工示例

假设您具有三个不同的函数,每个函数发送一种特定类型的消息(例如sendTemperature(),sendHumidity(),sendAirPressure()),并且在每个函数中调用MQTTAsync_sendMessage,并且在每次传递后都希望调用匹配的回调函数,那么您将选择MQTTAsync_onSuccess.然后,您无需跟踪MQTTAsync_token并将其与回调相关联.

Suppose you have three different functions, each sending a specific type of message (e.g. sendTemperature(), sendHumidity(), sendAirPressure()) and in each function you call MQTTAsync_sendMessage, and after each delivery you want to call a matching callback function, then you would choose MQTTAsync_onSuccess. Then you do not need to keep track of MQTTAsync_token and associate that with your callbacks.

例如,如果您想改为实现日志记录功能,则使用MQTTAsync_deliveryComplete会更有用,因为它被称为每次交付.

For example, if you want to implement a logging function instead, it would be more useful to use MQTTAsync_deliveryComplete because it is called for every delivery.

当然,可以想象一个人希望同时具有特定的动作和通用的记录功能,因此在这种情况下,这两种变体可以同时使用.

And of course one can imagine that one would want to have both the specific one with some actions and the generic one for logging, so in this case both variants could be used at the same time.

文档

您应该注意,MQTTAsync_deliveryComplete在其文档中明确声明它考虑了服务质量集. MQTTAsync_onSuccess文档中不是这种情况,但是当然,这并不意味着在实现中不会这样做.但是,如果这很重要,则应显式检查源代码.

You should note that MQTTAsync_deliveryComplete explicitly states in its documentation that it takes into account the Quality of Service Set. This is not the case in the MQTTAsync_onSuccess documentation, but of course it does not mean that this is not done in the implementation. But if this is important, you should explicitly check the source code.

这篇关于MQTTAsync_onSuccess和MQTTAsync_deliveryComplete回调之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆