The problem is, when the client sends it's first message after getting a message, it can no longer receive and reply with a message.
Here is the code:
public void demo() {
try {
client = new MqttClient("tcp://broker:1883", "Sending");
client.connect();
client.setCallback(this);
client.subscribe("receive");
} catch (MqttException e) {}
}
@Override
public void messageArrived(String topic, MqttMessage message)
throws Exception {
message.setPayload("I'm replying".getBytes());
client.publish("publish", message);
}
I have something similar in Android development and it works as a charm. Paho 3-1.0.2
You should not publish new messages from with in the massageArrived
callback.
Use something like a AsyncTask
to do the publish from a separate thread
Edit, sorry, not 100% awake, miss read the on Android bit. rest still holds, just need to use a Excutor
and Runnable
rather than AsyncTask for normal Java
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.