我对MQTT协议感兴趣,我已经知道Web后端的基本知识但又很新。因此,我想知道如何接收来自经纪人的消息。 这是我正在尝试的代码:
public class mqttHelper {
public IMqttClient publisher;
public String hostAddress;
public int port =1883;
public String publisherId = UUID.randomUUID().toString();
public mqttHelper(String hostAddress, int port) throws MqttException {
this.hostAddress=hostAddress;
this.port=port;
publisher= new MqttClient(hostAddress+':'+port,publisherId);
MqttConnectOptions options = new MqttConnectOptions();
options.setAutomaticReconnect(true);
options.setCleanSession(true);
options.setConnectionTimeout(10);
publisher.connect(options);
/*
//For receiving messages:
CountDownLatch receivedSignal = new CountDownLatch(10);
subscriber.subscribe(EngineTemperatureSensor.TOPIC, (topic, msg) -> {
byte[] payload = msg.getPayload();
// ... payload handling omitted
receivedSignal.countDown();
});
receivedSignal.await(1, TimeUnit.MINUTES);
*/
}