Ssl 如何通过Javascript将TLS与Paho MQTT一起使用?

Ssl 如何通过Javascript将TLS与Paho MQTT一起使用?,ssl,mqtt,mosquitto,paho,Ssl,Mqtt,Mosquitto,Paho,我目前在我的网站上使用的代码 var client = null; var device_is_on = null; var hostname = "********"; var port = "8003"; var clientId = "mqtt_js_" + parseInt(Math.random() * 100000, 10); var device_topic = "stat/Device_001/POWER";

我目前在我的网站上使用的代码

var client       = null;
var device_is_on    = null;
var hostname       = "********";
var port           = "8003";
var clientId       = "mqtt_js_" + parseInt(Math.random() * 100000, 10);
var device_topic     = "stat/Device_001/POWER";
var status_topic   = "cmnd/Device_001/power";


function connect(){
    client = new Paho.MQTT.Client(hostname, Number(port), clientId);

    client.onConnectionLost = onConnectionLost;
    client.onMessageArrived = onMessageArrived;

    var options = {
        useSSL: true,
        userName : "***",
        password : "********",
        onSuccess: onConnect,
        onFailure: onFail
    };
    client.connect(options);
}


function onConnect(context) {
    options = {qos:0}
    client.subscribe(device_topic, options);
    client.subscribe(status_topic, options);

    var payloadd = "6";

    message = new Paho.MQTT.Message(payloadd);
    message.destinationName = status_topic;
    message.retained = true;
    client.send(message);
}

function onFail(context) {
}

function onConnectionLost(responseObject) {
    if (responseObject.errorCode !== 0) {
        window.alert("Connection Lost!\nPlease Refresh.");
    }
}

function onMessageArrived(message) {

    if (message.destinationName == device_topic){ 
        var temperature_heading = document.getElementById("device_display");
        temperature_heading.innerHTML = "Air Conditioner: " + message.payloadString;
        if (message.payloadString == "ON" || message.payloadString == "o"){
            device_is_on = true;
        } else {
            device_is_on = false;
        }
    }
}

function device_toggle(){
    if (device_is_on){
        var payload = "off";
        device_is_on = false;
    } else {
        var payload = "on";
        device_is_on = true;
    }

    message = new Paho.MQTT.Message(payload);
    message.destinationName = status_topic;
    message.retained = true;
    client.send(message);
}
我应该在“var选项”部分下放置什么?目前我在Google Chrome的控制台中收到错误ERR_CERT_AUTHORITY_INVALID

注1:这段代码在http上运行得非常好,但我正在转换为https

注2:我使用MOSQUITO作为我的MQTT代理


非常感谢您的帮助。

TLS javascript paho客户端可用:

看起来您正在使用自签名证书。您的浏览器不信任此选项,因此它将无法连接,从而引发您显示的错误

您有两种选择:

  • 将证书导入浏览器并将其标记为受信任(执行此操作的方式因使用的浏览器而异)。这只对测试/开发非常有用,因为普通用户不应该导入随机证书,因为这会使他们面临各种安全问题

  • 为您的网站和代理获取真正可信的证书。最简单/最便宜的方法是使用。然后,您可以配置MOSQUITO以使用此证书


  • 错误证书颁发机构无效:什么是服务器证书?HTTPS客户端需要知道它的CA。我使用OpenSSL生成了证书。这基本上是一个仅链接的答案。一旦链接断开,它就没用了。