首页 文章

MQTT订阅客户端在连接后不会收到消息

提问于
浏览
2

我正在努力学习MQTT并一直在玩它 . 我写了一个发布客户端和一个订阅客户端(见下文) .

如果我运行订阅客户端然后运行发布客户端(当订阅正在运行时),那么一切正常 . 我的订阅客户端正确接收发布到该主题的消息 .

但是,如果我首先运行发布客户端(即我向主题发布消息)然后运行订阅客户端,则不会收到任何消息 .

换句话说,如果我先连接子客户端然后在连接子客户端时使用pub客户端发布消息,一切正常 . 但是,如果我先发布消息,然后连接到我的子客户端,则不会收到任何消息 . 我的理解是,一旦我与客户端连接并订阅该主题,我就会收到有关该主题的消息 .

我发现了类似的问题:Cannot receive already published messages to subscribed topic on mqtt paho,虽然这种情况看起来有点不同 . 我've tried changing different QoS setting or cleanSession flag, but that didn' t解决了这个问题 .

任何帮助,将不胜感激!

发布客户:

public class MQTT_Client_Pub implements MqttCallback{

MqttClient client;

public static void main(String[] args) {

    new MQTT_Client_Pub().mqttPub();
}

public void mqttPub(){
    try {
        this.setConnection();

        // Connect
        client.connect();

        // Create new message
        MqttMessage message = new MqttMessage();
        message.setPayload("A single test message from b112358".getBytes());
        message.setQos(0);

        // Publish message to a topic
        System.out.println("Publishing a message.");
        client.publish("pahodemo/test/b112358", message);

        // Disconnect
        client.disconnect();

      } catch (MqttException e) {
        e.printStackTrace();
      } catch (Exception e){
        e.printStackTrace();
      }
}

public void setConnection(){
    // Client
    try{
        client = new MqttClient("tcp://iot.eclipse.org:1883", "mqtt_test_b112358_pub");
    } catch (MqttException e) {
        e.printStackTrace();
    }

    // Connection Options
    MqttConnectOptions options = new MqttConnectOptions();

    // Set the will
    options.setWill("pahodemo/clienterrors", "CRASHED - CONNECTION NOT CLOSED CLEANLY".getBytes(),2,true);

    // Set Callback
    client.setCallback(this);
}

public void deliveryComplete(IMqttDeliveryToken token) {
    System.out.println("Message delivered to the broker.");
}

public void messageArrived(String topic, MqttMessage message) throws Exception {}

public void connectionLost(Throwable cause) {}

}

订阅客户:

public class MQTT_Client_Sub implements MqttCallback{

MqttClient client;

public static void main(String[] args) {

    new MQTT_Client_Sub().mqttSub();

}

public void mqttSub(){
    try {
        // Set connection
        this.setConnection();

        // Connect
        client.connect();

        // Subscribe

        client.subscribe("pahodemo/test/b112358", 0);
        // Disconnect
        // client.disconnect();

      } catch (MqttException e) {
        e.printStackTrace();
      }
}

public void setConnection(){
    try {
        // Client
        client = new MqttClient("tcp://iot.eclipse.org:1883", "mqtt_test_b112358_sub");
    } catch (MqttException e) {
        e.printStackTrace();
    }

    // Connection Options
    MqttConnectOptions options = new MqttConnectOptions();
    options.setCleanSession(false);

    // Set the will
    options.setWill("pahodemo/clienterrors", "CRASHED - CONNECTION NOT CLOSED CLEANLY".getBytes(),2,true);

    client.setCallback(this);
}

public void deliveryComplete(IMqttDeliveryToken token) {}

public void messageArrived(String topic, MqttMessage message) throws Exception {
    System.out.println("Message Arrived: " + message.getPayload() + " on tipic: " + topic.getBytes());
}

public void connectionLost(Throwable cause) {}

}

1 回答

  • 4

    在订户连接和订阅之前发布的消息将仅在以下两种情况下发送

    • 当邮件发布为保留时 . 这意味着该主题的最后一条消息将在订阅时传递给新订户 . 这只会传递最后一条消息 .

    • 如果客户端先前已连接并订阅,则已断开连接 . 然后发布一条消息,客户端再次使用cleansession = false连接 . (订阅时为QOS1 / 2)

    这可能会有所帮助:http://www.thingsprime.com/?p=2897

相关问题