因为之前是在rabbitmq的插件“RabbitMQ Web MQTT plugin ”中看到使用了mqttws31.js的实例,由于对mqttws31不了解,网上下载了连接成功,但断开后不能自动重连。最后去官方看下了下iusue(),提到:
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”分支:
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"});