libmosquittopp-示例客户端挂在loop_stop()方法上
libmosquittopp - sample client hangs on loop_stop() method
我正在尝试为我的家庭应用程序创建一个简单的mqtt客户端,并且我正在使用libmosquittopp(这是libmosquitto的C 版本)。
该库的文档没有太多的文档,但是我找到了两个示例(此处和此处),这些示例帮助我为" MQTTWRAPPER"类创建了代码。
这是我的代码:
mqttwrapper.h:
#pragma once
#include <mosquittopp.h>
#include <string>
class MQTTWrapper : public mosqpp::mosquittopp
{
public:
MQTTWrapper(const char* id, const char* host_, int port_);
virtual ~MQTTWrapper();
void myPublish(std::string topic, std::string value);
private:
void on_connect(int rc);
void on_publish(int mid);
std::string host;
int port;
};
mqttwrapper.cpp
#include "MQTTWrapper.h"
#include <iostream>
MQTTWrapper::MQTTWrapper(const char* id, const char* host_, int port_) :
mosquittopp(id), host(host_), port(port_)
{
mosqpp::lib_init();
int keepalive = 10;
if (username_pw_set("sampleuser", "samplepass") != MOSQ_ERR_SUCCESS) {
std::cout << "setting passwd failed" << std::endl;
}
connect_async(host.c_str(), port, keepalive);
if (loop_start() != MOSQ_ERR_SUCCESS) {
std::cout << "loop_start failed" << std::endl;
}
}
MQTTWrapper::~MQTTWrapper()
{
std::cout << "1" << std::endl;
if (loop_stop() != MOSQ_ERR_SUCCESS) {
std::cout << "loop_stop failed" << std::endl;
}
std::cout << "2" << std::endl;
mosqpp::lib_cleanup();
std::cout << "3" << std::endl;
}
void MQTTWrapper::on_connect(int rc)
{
std::cout << "Connected with code " << rc << "." << std::endl;
}
void MQTTWrapper::myPublish(std::string topic, std::string value) {
int ret = publish(NULL, topic.c_str(), value.size(), value.c_str(), 1, false);
if (ret != MOSQ_ERR_SUCCESS) {
std::cout << "Sending failed." << std::endl;
}
}
void MQTTWrapper::on_publish(int mid) {
std::cout << "Published message with id: " << mid << std::endl;
}
和我的main():
#include <iostream>
#include <string>
#include "MQTTWrapper.h"
int main(int argc, char *argv[])
{
MQTTWrapper* mqtt;
mqtt = new MQTTWrapper("Lewiatan IoT", "my.cloudmqtt.host", 12345);
std::string value("Test123");
mqtt->myPublish("sensors/temp", value);
std::cout << "about to delete mqtt" << std::endl;
delete mqtt;
std::cout << "mqtt deleted" << std::endl;
return 0;
}
对不起,这么多代码。
我的问题是,当我进行编译并执行时 - 我的应用程序在loop_stop()方法上无限悬挂(我只等待9分钟)在mqttwrapper destructor中。
用libmosquittopp 1.4.8(Debian软件包)进行测试,然后在删除它之后 - 从Github中使用1.4.9版。
loop_start()
和loop_stop(bool force=false)
应启动/停止处理消息传递的单独线程。
我已经用强制停止(loop_stop(true)
)对其进行了测试,但是这样我的应用程序停止了,并且不发布任何数据。loop_stop()
另一方面发布数据,但随后停止。
控制台输出(make && ./executable
):
g++ -c MQTTWrapper.cpp
g++ -c main.cpp
g++ -o executable main.o MQTTWrapper.o -lmosquittopp
about to delete mqtt
1
Connected with code 0.
Published message with id: 1
(here it hangs infinitely...)
我的问题:
为什么此loop_stop()
悬挂以及如何修复它?
(任何文档/教程/示例赞赏)
在loop_stop()
之前尝试调用disconnect()
。您还应该记住,您正在有效地这样做:
connect_async();
loop_start();
loop_stop();
客户端甚至可能没有机会连接,也可能没有在告诉它停止之前启动线程。
值得考虑在回调中运行操作:
on_connect -> call publish
on_publish -> call disconnect
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 使用std::函数映射对象方法
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- C++从另一个类访问公共静态向量的正确方法是什么
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 没有为自己的结构调用列表推回方法
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在类定义之后定义一个私有方法
- 枚举环境变量的惯用C++14/C++17方法
- 初始化具有非默认构造函数的std::数组项的更好方法