mqtt sub default callback: test { "msg": "hello", "sa":"haha", "name":"GG Bond22" }
时间: 2023-08-09 19:05:57 浏览: 45
这个问题涉及到MQTT的订阅和回调函数。根据提供的引用内容,我们可以看到有一个mqtt sub default callback的测试例子。在这个例子中,订阅的主题是"test",当收到消息时,回调函数会被调用。回调函数的参数是一个JSON对象,包含了"msg"、"sa"和"name"三个字段。根据提供的例子,收到的消息是:
{
"msg": "hello",
"sa":"haha",
"name":"GG Bond22"
}
所以,回调函数会被调用,并且参数是这个JSON对象。
相关问题
MQTT Ping! ERROR: MQTT NOT ALIVE OR MSG LIMIT
MQTT Ping是一种用于保持MQTT连接的机制。当客户端与MQTT代理建立连接后,客户端可以定期发送Ping消息以确认连接是否仍然有效。如果代理在规定的时间内未收到来自客户端的Ping消息,则代理将认为该连接已断开,并关闭连接。Ping消息的目的是确保MQTT连接保持活动状态,防止在长时间空闲时被关闭。
c++ paho mqtt库 mqtt::topic 发布 json对象
以下是使用C++ paho mqtt库中的mqtt::topic发布json对象的示例代码:
```c++
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <json/json.h>
#include "mqtt/async_client.h"
using namespace std;
const string ADDRESS("tcp://localhost:1883");
const string CLIENTID("AsyncPublisher");
const string TOPIC("test");
const int QOS = 1;const int TIMEOUT = 10000L;
class callback : public virtual mqtt::callback
{
public:
virtual void connection_lost(const std::string& cause)
{
cout << "\nConnection lost" << endl;
if (!cause.empty())
cout << "\tcause: " << cause << endl;
}
virtual void delivery_complete(mqtt::delivery_token_ptr token)
{
cout << "\n\tDelivery complete for token: "
<< (token ? token->get_message_id() : -1) << endl;
}
};
int main(int argc, char* argv[])
{
mqtt::async_client client(ADDRESS, CLIENTID);
mqtt::connect_options connOpts;
connOpts.set_keep_alive_interval(20);
connOpts.set_clean_session(true);
callback cb;
client.set_callback(cb);
try {
mqtt::token_ptr conntok = client.connect(connOpts);
conntok->wait_for_completion();
Json::Value root;
root["name"] = "John";
root["age"] = 25;
root["email"] = "john@example.com";
string json_str = root.toStyledString();
mqtt::message_ptr pubmsg = mqtt::make_message(TOPIC, json_str);
pubmsg->set_qos(QOS);
mqtt::delivery_token_ptr pubtok = client.publish(pubmsg);
pubtok->wait_for_completion(TIMEOUT);
cout << "Message '" << json_str << "' published to topic '"
<< TOPIC << "'" << endl;
client.disconnect()->wait_for_completion();
}
catch (const mqtt::exception& exc) {
cerr << exc.what() << endl;
return 1;
}
return 0;
}
```
在这个示例中,我们使用了Jsoncpp库来创建一个Json对象,并将其转换为字符串。然后,我们使用mqtt::make_message()函数创建一个mqtt::message_ptr对象,该对象包含要发布的主题和消息。最后,我们使用mqtt::async_client类的publish()函数将消息发布到主题上。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)