1
0
mirror of https://github.com/eclipse/paho.mqtt.cpp.git synced 2025-05-09 19:31:22 +08:00

Implemented new event consumer examples and fixed some bugs with it.

This commit is contained in:
fpagliughi 2024-07-06 20:53:21 -04:00
parent 0b85abd745
commit 3ef1292f45
4 changed files with 37 additions and 27 deletions

View File

@ -33,6 +33,8 @@ set(EXECUTABLES
async_subscribe async_subscribe
async_consume async_consume
async_consume_v5 async_consume_v5
async_message_consume
async_message_consume_v5
data_publish data_publish
mqttpp_chat mqttpp_chat
multithr_pub_sub multithr_pub_sub

View File

@ -62,12 +62,6 @@ int main(int argc, char* argv[])
.automatic_reconnect() .automatic_reconnect()
.finalize(); .finalize();
// The client will handle automatic reconnects, but we add this
// callbacks to let the user know when we're reconnected.
cli.set_connected_handler([](const std::string&) {
cout << "\n*** Connected ***" << endl;
});
try { try {
// Start consumer before connecting to make sure to not miss any messages // Start consumer before connecting to make sure to not miss any messages
@ -96,13 +90,22 @@ int main(int argc, char* argv[])
cout << "\nWaiting for messages on topic: '" << TOPIC << "'" << endl; cout << "\nWaiting for messages on topic: '" << TOPIC << "'" << endl;
// The client handles automatic reconnects, but we monitor
// the events here to report them to the user.
while (true) { while (true) {
auto msg = cli.consume_message(); auto evt = cli.consume_event();
if (const auto* p = std::get_if<mqtt::message_arrived_event>(&evt)) {
auto& msg = p->msg;
if (msg) if (msg)
cout << msg->get_topic() << ": " << msg->to_string() << endl; cout << msg->get_topic() << ": " << msg->to_string() << endl;
else }
else if (std::holds_alternative<mqtt::connected_event>(evt))
cout << "\n*** Connected ***" << endl;
else if (std::holds_alternative<mqtt::connection_lost_event>(evt))
cout << "*** Connection Lost ***" << endl; cout << "*** Connection Lost ***" << endl;
else
cout << "???" << endl;
} }
} }
catch (const mqtt::exception& exc) { catch (const mqtt::exception& exc) {

View File

@ -60,15 +60,6 @@ int main(int argc, char* argv[])
.finalize(); .finalize();
try { try {
cli.set_connection_lost_handler([](const std::string&) {
cout << "*** Connection Lost ***" << endl;
});
cli.set_disconnected_handler([](const mqtt::properties&, mqtt::ReasonCode reason) {
cout << "*** Disconnected. Reason [0x" << hex << int{reason} << "]: " << reason
<< " ***" << endl;
});
// Start consumer before connecting to make sure to not miss messages // Start consumer before connecting to make sure to not miss messages
cli.start_consuming(); cli.start_consuming();
@ -105,11 +96,27 @@ int main(int argc, char* argv[])
cout << "\nWaiting for messages on topic: '" << TOPIC << "'" << endl; cout << "\nWaiting for messages on topic: '" << TOPIC << "'" << endl;
while (true) { while (true) {
auto msg = cli.consume_message();
if (!msg) auto evt = cli.consume_event();
break;
if (const auto* p = std::get_if<mqtt::message_arrived_event>(&evt)) {
auto& msg = p->msg;
if (msg)
cout << msg->get_topic() << ": " << msg->to_string() << endl; cout << msg->get_topic() << ": " << msg->to_string() << endl;
} }
else if (std::holds_alternative<mqtt::connected_event>(evt)) {
cout << "\n*** Connected ***" << endl;
}
else if (std::holds_alternative<mqtt::connection_lost_event>(evt)) {
cout << "*** Connection Lost ***" << endl;
break;
}
else if (const auto* p = std::get_if<mqtt::disconnected_event>(&evt)) {
cout << "*** Disconnected. Reason [0x" << hex << int{p->reasonCode}
<< "]: " << p->reasonCode << " ***" << endl;
break;
}
}
// If we're here, the client was almost certainly disconnected. // If we're here, the client was almost certainly disconnected.
// But we check, just to make sure. // But we check, just to make sure.
@ -120,9 +127,6 @@ int main(int argc, char* argv[])
cli.disconnect()->wait(); cli.disconnect()->wait();
cout << "OK" << endl; cout << "OK" << endl;
} }
else {
cout << "\nClient was disconnected" << endl;
}
} }
catch (const mqtt::exception& exc) { catch (const mqtt::exception& exc) {
cerr << "\n " << exc << endl; cerr << "\n " << exc << endl;

View File

@ -857,8 +857,9 @@ void async_client::start_consuming()
nullptr nullptr
); );
if (rc != MQTTASYNC_SUCCESS) check_ret(rc);
throw exception(rc); check_ret(::MQTTAsync_setConnected(cli_, this, &async_client::on_connected));
check_ret(::MQTTAsync_setDisconnected(cli_, this, &async_client::on_disconnected));
} }
void async_client::stop_consuming() void async_client::stop_consuming()