提交 74dfdf95 authored 作者: blu's avatar blu

mqtt api

上级 ddf5eabc
......@@ -13,7 +13,6 @@ extern "C" {
#include "MQTTAsync.h"
}
using namespace std;
void on_connected(void* context, MQTTAsync_successData* response);
......@@ -25,7 +24,6 @@ void on_conn_fail(void* context, MQTTAsync_failureData* response);
void on_subscribed_fail(void* context, MQTTAsync_failureData* response);
void on_sent(void* context, MQTTAsync_successData* response);
struct StrException : public std::exception
{
std::string s;
......@@ -90,14 +88,17 @@ class MqttHelper {
MQTTAsync_disconnectOptions disc_opts = MQTTAsync_disconnectOptions_initializer;
int rc;
int ch;
string msg;
if(MQTTASYNC_SUCCESS != MQTTAsync_create(&client, mqtt_addr.c_str(), id.c_str(), MQTTCLIENT_PERSISTENCE_NONE, NULL)){
spdlog::error("failed to async create mqtt");
//TODO
msg = "failed to async create mqtt";
spdlog::error(msg);
throw StrException(msg);
}
if(MQTTASYNC_SUCCESS != MQTTAsync_setCallbacks(client, this, on_connlost, on_msgarrvd, NULL)){
spdlog::error("failed to async setcallbacks");
//TODO
msg = "failed to async setcallbacks";
spdlog::error(msg);
throw StrException(msg);
}
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
......@@ -107,8 +108,9 @@ class MqttHelper {
spdlog::info("trying to connect");
if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start connect, return code %d\n", rc);
exit(EXIT_FAILURE);
msg = fmt::format("Failed to start connect: {}", MQTTAsync_strerror(rc));
spdlog::error(msg);
throw StrException(msg);
}
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论