与用户名和用户连接时,paho java客户端是否存在问题?密码??
从paho JS客户端可以工作但是从java客户端却没有。 我有这个代码
MqttConnectOptions conOpt = new MqttConnectOptions();
conOpt.setCleanSession(false);
conOpt.setUserName("test5");
conOpt.setPassword("123".toCharArray());
MqttDefaultFilePersistence filePersistence = new MqttDefaultFilePersistence("/home/manish/Downloads/mqttPersist");
client = new MqttAsyncClient(appProps.getProperty("mqtt.broker"),
appProps.getProperty("mqtt.clientId"), filePersistence);
client.setCallback(this);
client.connect(conOpt, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken imt) {
try {
client.subscribe(Constants.INTERNAL_TOPICS, Constants.INTERNAL_TOPIC_QOS);
} catch (MqttException ex) {
ex.printStackTrace();
}
}
@Override
public void onFailure(IMqttToken imt, Throwable thrwbl) {
thrwbl.printStackTrace();
}
});
我收到此异常
Bad user name or password (4)
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:28)
at org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:885)
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:118)
at java.lang.Thread.run(Thread.java:745)
emqt console
06:47:36.456 [error] Client(notification_subs_bot@127.0.0.1:50741): Username 'undefined' login failed for username_or_password_undefined
06:47:36.463 [error] Client(notification_subs_bot@127.0.0.1:50742): Username 'undefined' login failed for username_or_password_undefined
根据paho文档public void setPassword(char [] password)所以这里我将char []作为paasword传递
我正在使用emqttd
经纪人
答案 0 :(得分:0)
答案 1 :(得分:0)
这是使用eclipse paho mqtt库发布到mqtt代理的代码。
import com.fasterxml.jackson.core.JsonProcessingException;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class GpsPublish {
public static void main(String[] args) {
String uri = "tcp://000.000.000.000:1883";
String deviceName = "SENSE2";
String deviceToken = "IHAUD6kvW6C1Zp10uJm4";
String topic = "v1/devices/me/telemetry";
String clientId = MqttAsyncClient.generateClientId();
MqttClientPersistence persistence = new MemoryPersistence();
ObjectMapper MAPPER = new ObjectMapper();
String jsonString = "{\"latitude\":51.520008, \"longitude\":13.304954, \"temperature\":31.5, \"active\": false}";
try {
MqttAsyncClient client = new MqttAsyncClient(uri, clientId, persistence);
MqttConnectOptions options = new MqttConnectOptions();
options.setUserName(deviceToken);
client.connect(options, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken iMqttToken) {
System.out.println("[{" + deviceName + "}] connected to Thingsboard!");
}
@Override
public void onFailure(IMqttToken iMqttToken, Throwable e) {
System.out.println("[{" + deviceName + "}] failed to connect to Thingsboard!");
// System.out.println("reason " + e.getReasonCode());
System.out.println("msg " + e.getMessage());
System.out.println("loc " + e.getLocalizedMessage());
System.out.println("cause " + e.getCause());
System.out.println("excep " + e);
e.printStackTrace();
}
}).waitForCompletion();
JsonNode data = MAPPER.readTree(jsonString);
MqttMessage msg = new MqttMessage(MAPPER.writeValueAsString(data).getBytes(StandardCharsets.UTF_8));
IMqttDeliveryToken deliveryToken = client.publish(topic, msg, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
System.out.println("Data updated!");
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
System.out.println("[{" + deviceName + "}] Data update failed!");
System.out.println(exception.getMessage());
}
});
deliveryToken.waitForCompletion();
System.out.println("[{" + deviceName + "}] delivery completed at Thingsboard!");
client.disconnect().waitForCompletion();
System.out.println("[{" + deviceName + "}] disconnected to Thingsboard!");
} catch (MqttException e) {
System.out.println("Failed to connect to the server: " + e);
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}