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

mqtt api

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