如何使用flespi连接和发布Java配置单元MQTTClient?
flespi.io使用身份验证令牌作为用户名,如果需要,还可以将该令牌设置为密码。如何使用HiveMQ Java实现连接到flespi如何使用flespi连接和发布Java配置单元MQTTClient?,java,mqtt,hivemq,Java,Mqtt,Hivemq,flespi.io使用身份验证令牌作为用户名,如果需要,还可以将该令牌设置为密码。如何使用HiveMQ Java实现连接到flespi Mqtt3AsyncClient client = MqttClient.builder() .useMqttVersion3() .identifier(UUID.randomUUID().toString()) .simpleAuth()
Mqtt3AsyncClient client = MqttClient.builder()
.useMqttVersion3()
.identifier(UUID.randomUUID().toString())
.simpleAuth()
.username(mqttClientConfig.getUser())
.password(mqttClientConfig.getPassword().getBytes()).applySimpleAuth()
.serverHost(mqttClientConfig.getBrokerHost())
.serverPort(mqttClientConfig.getBrokerPort())
.buildAsync();
client.connect()
.whenComplete((connAck, throwable) -> {
if (throwable != null) {
logger.error("MQTT connection error: ", throwable);
} else {
client.publishWith()
.topic(mqttClientConfig.getPublishTopic())
.payload(payload.getBytes())
.qos(MqttQos.fromCode(mqttClientConfig.getQos()))
.send()
.whenComplete((mqtt3Publish, subThrowable) -> {
if (subThrowable != null) {
logger.error("MQTT subscripton error: ", subThrowable);
} else {
logger.info("Published on: {}", mqttClientConfig.getTopic());
}
});
client.disconnect();
}
});
我得到一个错误:
INFO n.k.s.mqtt.HiveMqttClient - Connecting to mqtt.flespi.io:8883...
ERROR n.k.s.mqtt.HiveMqttClient - MQTT connection error:
com.hivemq.client.mqtt.exceptions.ConnectionClosedException: Server closed connection without DISCONNECT.
端口8883通常用于通过TLS的MQTT 如果是这样的话,那么您需要向构建器添加如下内容
.sslWithDefaultConfig()
请参阅HiveMQ教程