0

我正在开发 Eclipse 中的 MQTT 应用程序。我之前使用 mqtt-dashboard 作为公共代理,并且能够看到我在仪表板上发布的消息。由于某种原因,这个网站已经关闭,所以我切换到 mosquitto。我的代码是相同的,但我仍然无法向该代理发布消息。我的代码如下:

public static void main(String[] args) {

    String topic        = "home automation systems";
    String content      = "I am a test message";
    int qos             = 2;
    String broker       = "tcp://test.mosquitto.org:1883";
    String clientId     = "home automation";
    MemoryPersistence persistence = new MemoryPersistence();
    try {
            MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setCleanSession(true);
            System.out.println("Connecting to broker: "+broker);
            sampleClient.connect(connOpts);
            System.out.println("Connected");
            System.out.println("Publishing message: "+content);
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(qos);
            sampleClient.publish(topic, message);
            System.out.println("Message published");
            sampleClient.disconnect();
            System.out.println("Disconnected");
            System.exit(0);
        } 
        catch(MqttException me) {
            System.out.println("reason "+me.getReasonCode());
            System.out.println("msg "+me.getMessage());
            System.out.println("loc "+me.getLocalizedMessage());
            System.out.println("cause "+me.getCause());
            System.out.println("excep "+me);
            me.printStackTrace();
        }
}

}

我正在尝试在此仪表板上查看已发布的消息:http: //test-mosquitto.herokuapp.com/ 但看不到我的消息。如果我遗漏了什么,请纠正我。我是新手。谢谢。

4

1 回答 1

0

如果您希望一条消息显示在该“仪表板”上,则需要在发布时设置保留标志 - 该 Web 应用程序只能显示保留消息,而不是常规的临时消息。

于 2014-10-21T00:52:45.763 回答