js從物接入lot hub mqttws31獲取資料
阿新 • • 發佈:2018-12-06
<script type="text/javascript" src="mqttws31.js"></script>
var hostname = '', port = , clientId = '', timeout = , keepAlive = , cleanSession = true, ssl = true, userName = '', password = '', topic = ''; client = new Paho.MQTT.Client(hostname, port, clientId); //建立客戶端例項 var options = { invocationContext: { host : hostname, port: port, path: client.path, clientId: clientId }, timeout: timeout, keepAliveInterval: keepAlive, cleanSession: cleanSession, useSSL: ssl, userName: userName, password: password, onSuccess: onConnect, onFailure: function(){ console.log(12112); } }; client.connect(options); //連線伺服器並註冊連線成功處理事件 function onConnect() { console.log("onConnected"); client.subscribe(topic); //訂閱主題 //傳送訊息 message = new Paho.MQTT.Message(""); message.destinationName = topic; // client.send(message); } client.onConnectionLost = onConnectionLost; //註冊連線斷開處理事件 client.onMessageArrived = onMessageArrived; //註冊訊息接收處理事件 function onConnectionLost(responseObject) { if (responseObject.errorCode !== 0) { console.log("onConnectionLost:"+responseObject.errorMessage); console.log("連線已斷開"); } } function onMessageArrived(message) { console.log("收到訊息:"+message.payloadString); }