gpt4 book ai didi

java - 如何使用 Eclipse Paho 在 Java MQTT 客户端上接收消息时发布消息

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:32:05 24 4
gpt4 key购买 nike

我正在尝试使用 Eclipse Paho 在 Java 中的 MQTT 客户端上实现一些功能。目标是订阅一个主题,当收到消息时,客户端发送另一个主题的另一条消息。

这看起来很简单,但我有一个我无法解决的奇怪问题。这是我的代码:

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class MqttOperations implements MqttCallback {

MqttClient sampleClient;
MqttConnectOptions connOpts;

public MqttOperations() {
}

public static void main(String[] args) throws InterruptedException {
new MqttOperations().launchMqttClient();
}


public void launchMqttClient() throws InterruptedException {
try {
MemoryPersistence persistence = new MemoryPersistence();
sampleClient = new MqttClient("tcp://broker.mqttdashboard.com:1883", "iamaclient", persistence);
connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
sampleClient.connect(connOpts);
sampleClient.subscribe("topic/example/ofmessage");
sampleClient.setCallback(this);

} 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();
}
}


@Override
public void connectionLost(Throwable cause) {
// TODO Auto-generated method stub

}

@Override
public void messageArrived(String topic, MqttMessage message) throws MqttException
{
System.out.println("Received: " + message.toString());
try{
System.out.println("Publishing message: i am the answer");
MqttMessage ans = new MqttMessage("i am the answer".getBytes());
ans.setQos(2);
sampleClient.publish("topic/example/ofanswer", ans);
System.out.println("Message published");

}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();
}

}

@Override
public void deliveryComplete(IMqttDeliveryToken token) {

}

}

问题是,这个程序只能运行一次。收到消息后,将发送对此消息的答复,但似乎消息“已发布消息”从未显示在屏幕上,客户端也没有收到任何其他消息。我的印象是行 sampleClient.publish("topic/example/ofanswer", ans); 从未完成执行。有谁知道它是怎么来的以及如何解决我的问题?

最佳答案

我今天遇到了类似的问题。当我阅读 an other question with two connections我明白了:您需要两个 MqttClient 实例。一种用于发布,一种用于订阅。不幸的是,我没有找到有关该事实的文档。

顺便说一下。在我与两个客户端的第一个实现中,我给了他们相同的 ID(逻辑上应该是相同的连接)。但是第二个连接断开了第一个连接。当我开始使用两个不同的 ID 时,它开始起作用。

关于java - 如何使用 Eclipse Paho 在 Java MQTT 客户端上接收消息时发布消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31161740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com