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

由于以前是在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"});
相关文章
相关标签/搜索