I'm writing a mqtt app in C++ with Paho. I'm using Paho.mqtt C library that can be found here.
When I receive a message in the callback function, it calls another function to print the message. I want to be able to loop that function until a new message arrives.
bool start = false;
void loopMessage(std::string message){
start = true;
while(start){
std::out << "message: " << message;
}
}
int messageArrivedCallback(mqtt message){
start = false;
loopMessage(message);
}
The code above is not working; I can receive one message and run the while loop in the loopMessage
function. After that the client disconnects. I think this is because it is in blocking mode and the broker disconnects the client after the keep alive interval (I'm using MQTTClient). If I quickly send a new message before disconnection, the client doesn't receive it.
I tried to use the asynchronus client version (MQTTAsync) but it is giving me the same problem.
Any suggestions on how to do this?