[英]MQTT - Java Application Cannot see published message
我正在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/,但看不到我的消息。 如果我缺少某些東西,請糾正我。 我是新來的。 謝謝。
如果您希望某條消息顯示在該“儀表板上”,則需要使用保留標志進行發布-該Web應用程序只能顯示保留的消息,而不能顯示常規的臨時消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.