使用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"});


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM