java - MQTT subscribe client doesn't receive messages after connection -


I am trying to learn MCT and playing with it. I have written a client to publish and a client to subscribe (see below).

If I subscribe to the client and then I turn on the client (when the subscription is on), then everything works fine. My subscription customer receives the message published properly.

However, if I run the published client before (i.e. publishing a message to a topic) and then I subscribe to the client, I can not get the message.

In other words, if I first connect to all clients and then when the sub-client is connected, the Pub publishes the message with the client, then everything works fine However, if I first publish a message I'm connected to my sub-client, then I do not get any message. My understanding is that I should get the message which is once connected to the client and the subject Or once exist on the subject.

I found out that there is any such problem: although this case seems a little different. I have tried to change the separate QoS setting or clean session flag, but it did not solve this problem.

Any help would be appreciated!

Publish Client:

  The public class MQTT_Client_Pub implements the MqttCallback {MqttClient Client; Public static zero main (string [] args) {new MQTT_Client_Pub (). MqttPub (); } Public Zero mqttPub () Try {{this.setConnection ()}; // connect client.connect (); // Create new message MqttMessage message = new MqttMessage (); Message.setPayload ("single test message from b112358" .getBytes ()); Message.setQos (0); // Publish the message to the topic System.out.println ("Publishing the message."); Client publish ("Prodomo / Test / B1122358", Message); // Disconnect client.disconnect (); } Hold (MqttException e) {e.printStackTrace (); } Hold (exception e) {e.printStackTrace (); }} Public Zero Set Connection () {Try {Client} {Client = New MqttClient ("tcp: //iot.eclipse.org: 1883", "mqtt_test_b112358_pub"); } Hold (MqttException e) {e.printStackTrace (); } // Connection options MqttConnectOptions option = new MqttConnectOptions (); // Set Will option .setWill ("Powdemo / Climbers", "Crash - The connection has not been cleaned" .getBytes (), 2, true); // Set the callback client .Set callback (this); } Public Zero Distribution Complete (IMqttDeliveryToken Tokens) {System.out.println ("Message sent to the broker."); Public awkward message delivered (string subject, MqttMessage message) Exception {} throws public for zero connection (Iron reason) {}  

}

subscription Subscribe:

  The public class MQTT_Client_Sub applies the MqttCallback {MqttClient Client; Public static zero main (string [] args) {new MQTT_Client_Sub (). MqttSub (); } Public Zero mqttSub () {try set // Set this.setConnection (); // connect client.connect (); // subscribe customer Subscribe ("Prodomo / Test / B1122358", 0); // disconnect // client.disconnect (); } Hold (MqttException e) {e.printStackTrace (); }} Public Zero Set Connection () {try {// Client Client = New MqttClient ("tcp: //iot.eclipse.org: 1883", "mqtt_test_b112358_sub"); } Hold (MqttException e) {e.printStackTrace (); } // Connection options MqttConnectOptions option = new MqttConnectOptions (); Options.setCleanSession (wrong); // Set Will option .setWill ("Powdemo / Climbers", "Crash - The connection has not been cleaned" .getBytes (), 2, true); Client.setCallback (this); } Public Zero Distribution Complete (IMqttDeliveryToken Token) {} Public Null Out Received Message (String Subject, MqttMessage Message) Exception {System.out.println ("Message Accessed:" + message.getPayload () + "On Tip:" + GetBytes ()); } Public Zero Connection Knowledge (cause of throb) {}  

}

< P> Messages sent before the customer connecting and subscribing will be distributed only under the following 2 conditions

  1. When messages were continued, this means that the subject The last message will be given to a new customer at the time of subscription. This will only give the final message.

  2. If the customer was already connected and subscribed, it has been disconnected. A message is then published and the client connects with clenation = false again. (And when the subscription is on QOS1 / 2)

it can help:


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -