c++ - 使用 C++ 中的 mosquitto 将消息连接并发布到 AWS 端点
问题描述
我正在尝试执行这段代码以连接到 AWS 端点并使用 mosquitto 库发布消息:
void connecAndPublishtToAWS(const std::string &endpoint,
uint16_t port,
const std::string &cafile,
const std::string &certfile,
const std::string &keyfile,
const std::string &topic,
const std::string &message)
{
// Initialize library.
mosquitto_lib_init();
mosquitto *handler = mosquitto_new(endpoint.c_str(), true, nullptr);
mosquitto_loop_start(handler);
// Connect to endpoint.
handler->setIntegerOption(mosq_opt_t::MOSQ_OPT_TLS_USE_OS_CERTS, 1U);
mosquitto_tls_set(handler, cafile.c_str(), nullptr, certfile.c_str(), keyfile.c_str(), nullptr);
mosquitto_connect(handler, endpoint.c_str(), port, 10U);
// Publish to topic.
uint8_t times = 100;
while(times)
{
mosquitto_publish(handler, nullptr, topic.c_str(), message.size(),
message.c_str(), 2U, false);
::sleep(1u);
--times;
}
// Close the library.
mosquitto_loop_stop(handler, true);
mosquitto_destroy(handler);
mosquitto_lib_cleanup();
}
似乎它已连接(在我的真实代码中,我检查了返回码以检查连接或发布期间是否发生任何错误,并且连接期间没有问题)。
但是,当发布消息时,AWS 代理中没有收到这些消息(经过一些尝试,连接似乎关闭了,因为publish
函数返回错误 4 - MOSQ_ERR_NO_CONN
-)。
我还尝试使用 CLI 命令mosquitto_pub
(
知道我做错了什么吗?
解决方案
推荐阅读
- r - huxtable() 回归表不按小数对齐
- azure-stream-analytics - 在 Azure 流分析中使用参考数据时不生成输出
- spring-boot - 如何使用java8按分区从cassandra加载/读取数据?
- html - 关于如何使此文本 CSS 动画在小屏幕上响应的任何想法?
- c# - 我需要使用 linq 获取多个索引而不是一个
- sql - 如何创建在数据库中具有只读访问权限但对数据库中的模式具有所有权限的用户?
- node.js - 使用 Twilio API 发送大量文本
- javascript - 如何从 TextBox 中获取文本并将其放在 HTML 中的锚标记中
- spring-boot - 如何忽略 logback RollingFileAppender FileNotFoundException
- javascript - 文本框值 onchange 添加新列