因為之前是在rabbitmq的插件“RabbitMQ Web MQTT plugin ”中看到使用了mqttws31.js的實例,由於對mqttws31不了解,網上下載了連接成功,但斷開后不能自動重連。最后去官方看下了下iusue(https://github.com/eclipse/paho.mqtt.javascript/issues/48),提到:
1.定時器自動重連
2.通過reconnent參數實現自動重連。
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.js
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"});