使用Paho MQTT Js成功连接到Mosquitto代理后突然断开连接

时间:2020-05-01 13:00:08

标签: javascript ssl websocket mqtt mosquitto

我已成功使用Paho MQTT JS客户端1.1.0版本连接到我的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

1 个答案:

答案 0 :(得分:0)

“连接”消息之后的套接字错误几乎总是一个Auth问题-您的密码错误,您的userID错误,您的SSL证书对Mosquitto不知道,等等。仔细检查所有内容,看看是否能找到东西。