我遇到了错误:
与'wss://iot.XXXX.GG:8883 / mqtt'的WebSocket连接失败:在收到握手响应之前连接已关闭
在Windows 10上使用Javascript Paho库尝试通过SSL连接到远程Mosquitto代理时。
以下列表显示了我已经尝试过的内容:
<script type = "text/javascript" language = "javascript">
var mqtt;
var reconnectTimeout = 2000;
var host="iot.XXXX.GG" ;
var port=8883;
function onConnect() {
// Once a connection has been made, make a subscription and send a message.
console.log("Connected ");
message = new Paho.MQTT.Message("Hello World");
message.destinationName = "sensor1";
mqtt.send(message);
}
function MQTTconnect() {
console.log("connecting to "+ host +" "+ port);
mqtt = new Paho.MQTT.Client(host,port,"clientjs");
var options = {
useSSL:true,
timeout: 3,
userName:"abc",
password:"qweqwe",
onSuccess: onConnect
};
mqtt.connect(options);
};
</script>
预期结果应该是一条消息,提示“已连接。实际结果显示在本文开头,是我得到的错误。
顺便说一句,我的Mosquitto.conf文件是:
allow_anonymous false
password_file /etc/mosquitto/passwd
listener 1883 localhost
protocol mqtt
listener 8883
certfile /etc/letsencrypt/live/iot.XXXX.GG/cert.pem
cafile /etc/letsencrypt/live/iot.XXXX.GG/chain.pem
keyfile /etc/letsencrypt/live/iot.XXXX.GG/privkey.pem
# WebSockets - insecure
listener 8083
protocol websockets
#http_dir /home/ΧΧΧΧ/domains/iot.XXXX.GG/public_html
#certfile /etc/letsencrypt/live/iot.XXXX.GG/cert.pem
#cafile /etc/letsencrypt/live/iot.XXXX.GG/chain.pem
#keyfile /etc/letsencrypt/live/iot.XXXX.GG/privkey.pem
答案 0 :(得分:1)
Paho MQTT客户端只能连接到配置为通过WebSocket运行MQTT的代理。
您提供的mosquitto.conf
文件定义了3个侦听器。
如果要通过WebSockets和SSL使用MQTT从网页连接,则需要取消注释来自第3个侦听器的证书,并将页面中连接的端口更改为8083(而不是8883)