在我的代码中,有四个类:mainProgram,Coordinator类,Client Class和Callback Class。 mainProgram包含程序的主要方法,它创建一个Coordinator类的实例并调用它的start()方法。 后者创建客户端类的两个实例,发布者和订阅者,连接它们并将其中一个实例(订阅者)订阅到多个主题。 客户端类包含与mqtt有关的所有内容以及有关subscribe方法的Callback,我为此创建了一个自己的类,Callback Class在messageArrived方法中接收传入的mqtt消息。
这是主要代码:
public class mainProgram throws MqttException {
public static void main(String[] args) {
Coordinator coordinator=new Coordinator();
coordinator.start();
}}
这是Coordinator类的代码:
public class Coordinator {
Client publisher;
Client subscriber;
public Coordinator() {
// TODO Auto-generated constructor stub
}
public void start() throws MqttException {
Client publisher=new Client("publisher","tcp://192.168.0.10",0);
publisher.connect();
Client subscriber=new Client("subscriber","tcp://192.168.0.10",0);
subscriber.connect();
subscriber.subscribeToTopic(new String [] {"home/Sensor2","home/Sensor3"});
}
public void getMessage(String message) throws MqttPersistenceException, MqttException {
publisher.publish("home/Sensor4", message);
}}
这是客户类:
public class Client {
String brokerAddress;
String clientId;
int qos;
MqttClient mqttClient;
MqttConnectOptions conOpt;
MemoryPersistence persistence;
Coordinator coordinator;
public Client(String clientId, String brokerAddress, int qos) {
this.clientId=clientId;
this.brokerAddress=brokerAddress;
this.qos=qos;
this.setConOpt();
}
public void setConOpt() {
conOpt=new MqttConnectOptions();
conOpt.setKeepAliveInterval(30);
conOpt.setAutomaticReconnect(false);
}
public void connect() throws MqttException {
mqttClient=new MqttClient(brokerAddress,clientId,persistence);
this.mqttClient.setCallback(new Callback(this));
this.mqttClient.connect(this.conOpt);
while(this.mqttClient.isConnected()==false) {
System.out.println("Connecting");
System.out.println("...");
}
if(this.mqttClient.isConnected()==true){
System.out.println("Der Mqttclient "+this.mqttClient.getClientId()+" is now connected!");
}
}
public void subscribeToTopic(String [] topics) throws MqttException {
for(int i=0;i<topics.length;i++) {
this.mqttClient.subscribe(topics[i]);
}
}
public void publish(String topic, String message) throws MqttPersistenceException, MqttException {
MqttMessage mqttMessage=new MqttMessage(message.getBytes());
this.mqttClient.publish(topic, mqttMessage);
}
}
这是回调类:
public class Callback implements MqttCallback{
Client client;
public Callback(Client client) {
// TODO Auto-generated constructor stub
this.client=client;
}
@Override
public void connectionLost(Throwable arg0) {
// TODO Auto-generated method stub
System.out.println("Connection to server's lost");
}
@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
// TODO Auto-generated method stub
System.out.println("Message's delivered");
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
String ingoingMsg=message.toString();
System.out.println("until here all's fine");
Coordinator coordinator=new Coordinator();
coordinator.getMessage(ingoingMsg);
}
}
程序启动,创建客户端,当我通过MQTTfx向上述提及的主题发布消息时,在控制台中显示:
当我调用方法“coordinator.getMessage()”并且没有发布任何内容时,程序结束并且客户端“subscriber”的连接结束。
最后,程序应该像这样:
我到处寻找解决方案,但我在网上遇到的问题并不完全相同。 也许你有一些有启发性的提示......