由於以前是在rabbitmq的插件「RabbitMQ Web MQTT plugin 」中看到使用了mqttws31.js的實例,因爲對mqttws31不瞭解,網上下載了鏈接成功,但斷開後不能自動重連。最後去官方看下了下iusue(https://github.com/eclipse/paho.mqtt.javascript/issues/48),提到:javascript
1.定時器自動重連
2.經過reconnent參數實現自動重連。java
client.connect( { cleanSession : false, onSuccess : onConnectSuccess, onFailure : onFailedConnect, keepAliveInterval: 30, reconnect : true, // Enable automatic reconnect reconnectInterval: 10 // Reconnect attempt interval : 10 seconds } );
採用第二種方式的時候報錯了,查看了mqttws31.js源碼確實不太正確,因而去github上找了下,才發現有一個「automatic-reconnect」分支:
https://github.com/eclipse/paho.mqtt.javascript/blob/automatic-reconnect/src/mqttws31.jsgit
connectOptions = connectOptions || {} ; validate(connectOptions, {timeout:"number", userName:"string", password:"string", willMessage:"object", keepAliveInterval:"number", cleanSession:"boolean", useSSL:"boolean", invocationContext:"object", onSuccess:"function", onFailure:"function", hosts:"object", ports:"object", reconnect:"boolean", mqttVersion:"number"});