Мне удалось подключиться к моему брокеру Mosquitto с помощью версии 1.1.0 клиента Paho MQTT JS. Тем не менее, клиент отключается сразу после завершения подключения. До использования этой версии я использовал 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