我有以下部分代码:
package com.company;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import javax.security.auth.callback.Callback;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main implements MqttCallback {
private static String sTopic;
private static int iQos;
private static MqttClient mqttClient;
private static String sUsername;
private static Frame frame = new Frame();
public static void main(String[] args) throws MqttException {
frame.getConnect().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int iPort;
String sIp = frame.getBrokerAddressValue();
sUsername = frame.getUsernameValue();
try {
String broker = "tcp://"; //bridge and host
iPort = frame.getPortValue();
broker+=sIp+":"+iPort;
mqttClient = new MqttClient(broker, sUsername, new MemoryPersistence()); //URI, ClientId, Persistence
MqttConnectOptions connectOptions = new MqttConnectOptions();
connectOptions.setCleanSession(true);
System.out.println("Connecting to broker: "+broker);
mqttClient.connect();
System.out.println("Connected");
}catch (NumberFormatException exc){
System.out.println("Wrong port format");
} catch (MqttException e1) {
e1.printStackTrace();
}
}
});
frame.getSubscribe().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sTopic = frame.getTopicValue();
try {
mqttClient.subscribe(sTopic);
} catch (MqttException e1) {
e1.printStackTrace();
}
System.out.println("Subscribed");
}
});
frame.getPublish().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String sMessage = frame.getMessageValue();
MqttMessage message = new MqttMessage(sMessage.getBytes());
iQos = frame.getQosValue();
message.setQos(iQos);
try {
mqttClient.publish(sTopic,message);
} catch (MqttException e1) {
e1.printStackTrace();
}
System.out.println("Message published");
}
});
}
@Override
public void connectionLost(Throwable throwable) {
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception{
frame.getTextArea().setText(String.valueOf(message));
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
}
这是我实现mqtt客户端的一部分。方法getConnect正在处理单击“连接”按钮,方法getSubscribe正在处理单击“订阅”按钮,方法getPublish正在处理单击“发布”按钮。问题是:当我订阅该主题时,消息不要到达已订阅该主题的客户端。怎么了?
答案 0 :(得分:0)
如果我正确理解了您的问题,则您已订阅主题“ sTopic”,但是当有人向主题“ sTopic”发布消息时,您没有收到消息。
您确定Mqtt客户端已成功连接到代理吗?进行订阅呼叫之前,先使mqtt客户端已连接。
if( mqttClient.isConnected()) {
mqttClient.subscribe(sTopic);
}
也适用于发布。
if( mqttClient.isConnected()) {
mqttClient.publish(sTopic,message);
}
一旦这些调用结束,您应该在messageArrived回调方法中看到已发布的消息。
您需要将回调方法设置为mqttClient
clientCallback = new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
}
};
mqttClient.setCallback(clientCallback);