使用 Paho MQTT Js 成功连接到 Mosquitto 代理后突然断开连接
Posted
技术标签:
【中文标题】使用 Paho MQTT Js 成功连接到 Mosquitto 代理后突然断开连接【英文标题】:sudden disconnection after succesful connection to Mosquitto broker using Paho MQTT Js 【发布时间】:2020-05-01 13:00:08 【问题描述】:我已经成功地使用 1.1.0 版本的 Paho MQTT JS 客户端连接到我的 Mosquitto 代理。但是,客户端在完成连接后立即断开连接。在使用这个版本之前,我使用的是 1.0.1 并且它工作正常。这是我涉及的主要功能的代码:
function onConnectionLost()
console.log("connection lost");
document.getElementById("status").innerhtml = "Connection Lost";
connected_flag=0;
function onFailure(message)
console.log("Failed");
setTimeout(MQTTconnect, reconnectTimeout);
function onConnect()
connected_flag=1;
document.getElementById("status").innerHTML = "Connected";
console.log("on Connect "+connected_flag);
sub_topics();
function MQTTconnect(id)
var user = document.getElementById("user").value;
var pass = document.getElementById("pass").value;
//mqtt = new Paho.MQTT.Client(host,port,id);
mqtt = new Paho.Client("wss://192.168.1.42:9873/mqtt",id);
//document.write("connecting to "+ host);
var options =
timeout: 3,
useSSL: true,
userName: user,
password: pass,
onSuccess: onConnect,
onFailure: onFailure,
;
mqtt.onConnectionLost = onConnectionLost;
mqtt.onMessageArrived = onMessageArrived;
mqtt.onConnected = onConnected;
mqtt.connect(options);
return false;
function sub_topics()
mqtt.subscribe("es2a2/rad");
mqtt.subscribe("e12r/temp");
mqtt.subscribe("e92/hum");
return false;
Mosquitto 代理的日志:
1588334688: New client connected from 192.168.1.43 as 33c1olvtc78311srsavrgq53ce (p2, c1, k60, u'admin').
1588334689: Socket error on client mgqilf3dv8dktthb7u4mudlm5i, disconnecting.
编辑 1
Mosquitto.conf(使用 Mosquitto 1.6.8 版):
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
per_listener_settings true
listener 9873
protocol websockets
cafile /etc/mosquitto/ca_certificates/ca.crt
keyfile /etc/mosquitto/certs/server.key
certfile /etc/mosquitto/certs/server.crt
allow_anonymous false
password_file /etc/mosquitto/passwdfile
listener 8883
cafile /etc/mosquitto/ca_certificates/ca.crt
keyfile /etc/mosquitto/certs/server.key
certfile /etc/mosquitto/certs/server.crt
【问题讨论】:
Edit 包括你的mosquitto.conf
,这样我们就可以看到它是如何为 websocket 监听器设置的。还包括 mosquitto 版本信息
【参考方案1】:
“连接”消息之后的套接字错误几乎总是一个身份验证问题——您的密码错误、用户 ID 错误、Mosquitto 不知道您的 SSL 证书等。仔细检查所有内容,看看是否找到东西。
【讨论】:
以上是关于使用 Paho MQTT Js 成功连接到 Mosquitto 代理后突然断开连接的主要内容,如果未能解决你的问题,请参考以下文章
python paho mqtt客户端连接通过ssl/tls给出错误