使用javascript连接mqtt协议(自动重连问题)

因为之前是在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"});
posted @ 2018-12-27 14:01 ikoDotA 阅读(...) 评论(...) 编辑 收藏
ikoDotA's 新浪微博