Как работать с С++ Mosqutto?
мне нужен издатель\подписчик, тяжело идет чтение API Mosquitto, всё что получилось это найти вот такой код, тут в принципе понятно, но как из этого сделать издателя и подписчика не знаю, дайте совет
class myMosq : public mosqpp::mosquittopp
{
private:
const char * host;
const char * id;
const char * topic;
int port;
int keepalive;
void on_connect(int rc);
void on_disconnect(int rc);
void on_publish(int mid);
public:
myMosq(const char *id, const char * _topic, const char *host, int port);
~myMosq();
void send_message(const char * _message);
};
myMosq::myMosq(const char * _id, const char * _topic, const char * _host, int _port) : mosquittopp(_id)
{
mosqpp::lib_init(); // Mandatory initialization for mosquitto library
this->keepalive = 60000; // Basic configuration setup for myMosq class
this->id = _id;
this->port = _port;
this->host = _host;
this->topic = _topic;
on_connect(connect_async(host, port, keepalive)); // non blocking connection to broker request
loop_start(); // Start thread managing connection / publish / subscribe
};
myMosq::~myMosq() {
loop_stop(); // Kill the thread
on_disconnect(mosqpp::lib_cleanup());// Mosquitto library cleanup
}
void myMosq::send_message(const char * _message)
{
int ret = publish(NULL, this->topic, strlen(_message), _message, 1, true);
if (ret == MOSQ_ERR_SUCCESS) {
on_publish(ret);
}
else {
std::cout << ">> myMosq - Message (" << ret << ") succeed to be published " << std::endl;
}
}
void myMosq::on_connect(int rc)
{
if (rc == 0) {
std::cout << ">> myMosq - connected with server" << std::endl;
}
else {
std::cout << ">> myMosq - Impossible to connect with server(" << rc << ")" << std::endl;
}
}
int main()
{
setlocale(LC_ALL,"Rus");
myMosq mqtt("cid1", "topic\\", "127.0.0.1", 1883);
myMosq mqtt2("cid1", "topic\\", "127.0.0.1", 1883);
while (true)
{
std::string some;
std::cin >> some;
if (some == "end") break;
mqtt.send_message(some.c_str());
}
system("pause");
}