nanomq.h
#ifndef NANOMQ_H
#define NANOMQ_H
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
#include <stdio.h>
#include <MQTTClient.h>
#include <boost/thread.hpp>
#define ADDRESS "tcp://broker.emqx.io:1883"
#define USERNAME "xgdd"
#define PASSWORD "xgdd"
#define CLIENTID "mqttx_47b97142"
#define QOS 0
#define TIMEOUT 10000L
class NanoMQ
{
public:
NanoMQ();
///
/// \brief init 连接nanomq
/// \return -1-连接失败 0-连接成功
///
int init();
///
/// \brief quit 断开连接
///
void quit();
///
/// \brief sendInfo 发送信息
/// \param topic 节点
/// \param data 数据
///
void sendInfo(char *topic, char *data);
private:
MQTTClient client;
void publish(MQTTClient client, char *topic, char *payload);
int receive_subMessage();
static void receive_subMessage_thread(void* arg);
};
#endif // NANOMQ_H
nanomq.cpp
#include "nanomq.h"
NanoMQ::NanoMQ()
{
}
int NanoMQ::init()
{
//法二
char* subscribeTopic[] = {"testtopic/123", "testtopic/1234", "testtopic/12345"};
int m_qos[] = {0, 0, 0};
MQTTClient_create(&client, ADDRESS, CLIENTID, 0, NULL);
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
conn_opts.username = USERNAME;
conn_opts.password = PASSWORD;
if (MQTTClient_connect(client, &conn_opts) != MQTTCLIENT_SUCCESS) {
printf("Failed to connect\n");
return -1;
} else {
printf("Connected to MQTT Broker!\n");
}
//法一
// if (MQTTClient_subscribe(client, "testtopic/123", QOS) != MQTTCLIENT_SUCCESS)
// {
// printf("Failed to subscribe\n");
// }
// if (MQTTClient_subscribe(client, "testtopic/1234", QOS) != MQTTCLIENT_SUCCESS)
// {
// printf("Failed to subscribe\n");
// }
// if (MQTTClient_subscribe(client, "testtopic/12345", QOS) != MQTTCLIENT_SUCCESS)
// {
// printf("Failed to subscribe\n");
// }
//法二
if (MQTTClient_subscribeMany(client, 3, subscribeTopic, m_qos) != MQTTCLIENT_SUCCESS)
{
printf("Failed to subscribe\n");
}
boost::thread t(&NanoMQ::receive_subMessage_thread, this);
return 0;
}
void NanoMQ::quit()
{
MQTTClient_disconnect(client, TIMEOUT);
MQTTClient_destroy(&client);
}
void NanoMQ::sendInfo(char *topic, char *data)
{
publish(client, topic, data);
}
void NanoMQ::publish(MQTTClient client, char *topic, char *payload)
{
MQTTClient_message message = MQTTClient_message_initializer;
message.payload = payload;
message.payloadlen = strlen(payload);
message.qos = QOS;
message.retained = 0;
MQTTClient_deliveryToken token;
MQTTClient_publishMessage(client, topic, &message, &token);
MQTTClient_waitForCompletion(client, token, TIMEOUT);
printf("Send `%s`\n", payload);
}
int NanoMQ::receive_subMessage()
{
int topicLen;
int rc;
char *topic = "testtopic/123";
MQTTClient_message *message = NULL;
while (1) {
rc = MQTTClient_receive(client, &topic, &topicLen, &message, TIMEOUT);
if( rc == MQTTCLIENT_SUCCESS && topic!= NULL ){
printf("Message arrived \n");
printf(" topic: %s\n", topic);
printf(" message: %.*s\n", message->payloadlen, (char*)message->payload);
}
printf("MQTTClient receive message, return code %d\n", rc);
sleep(1);
}
return rc;
}
void NanoMQ::receive_subMessage_thread(void *arg)
{
NanoMQ *m_nanomq = (NanoMQ *)arg;
m_nanomq->receive_subMessage();
}
main.cpp
#include <iostream>
#include <thread>
#include "nanomq.h"
using namespace std;
NanoMQ nanomq;
void topic_1()
{
while(true) {
nanomq.sendInfo("testtopic/1", "this is test topic 1");
sleep(1);
}
}
void topic_2()
{
while(true) {
nanomq.sendInfo("testtopic/2", "this is test topic 2");
sleep(1);
}
}
int main(int argc, char *argv[])
{
cout<<"hello world"<<endl;
if(nanomq.init() < 0) {
cout<<"nanomq connect failed.."<<endl;
}
thread t1(&topic_1);
thread t2(&topic_2);
while (true) {
sleep(1);
}
nanomq.quit();
return 0;
}