⽹页端html使⽤mqttjs,JavaScript使⽤MQTT-农民⼦弟-博客园< html > < head > < meta charset ="utf-8" /> < title > title > < script src ="cdnjs.cloudflare/ajax/libs/paho-
mqtt/1.0.1/mqttws31.min.js" type ="text/javascript" > script > < script > var hostname = ' 127.0.0.1 ' , // '192.168.1.2', port = 8083 ,
clientId = ' client-mao2080 ' ,
timeout = 5 ,
keepAlive = 100 ,
cleanSession = false ,
ssl = false , // userName = 'mao2080', // password = '123', topic = ' /World ' ;
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 (e) {
console.log(e);
s = " {time: " + new Date().Format( " yyyy-MM-dd hh:mm:ss " ) + " , onFailure()} " ;
console.log(s);
}
};
console.log( " onConnected " );
s = " {time: " + new Date().Format( " yyyy-MM-dd hh:mm:ss " ) + " , onConnected()} " ;
console.log(s);
client.subscribe(topic);
}
console.log(responseObject);
s = " {time: " + new Date().Format( " yyyy-MM-dd hh:mm:ss " ) + " , onConnectionLost()} " ;
console.log(s); if (Code !== 0 ) {
console.log( " onConnectionLost: " + Message);
console.log( " 连接已断开 " );
}
} function onMessageArrived(message) {
s = " {time: " + new Date().Format( " yyyy-MM-dd hh:mm:ss " ) + " , onMessageArrived()} " ;
console.log(s);
console.log( " 收到消息: " + message.payloadString);
} function send() { var s = ElementById( " msg " ).value; if (s) {
s = " {time: " + new Date().Format( " yyyy-MM-dd hh:mm:ss " ) + " , content: " + (s) + " , from: web console} " ;
message = new Paho.MQTT.Message(s);
message.destinationName = topic;
client.send(message);
}
} var count = 0 ; function start() {
message = new Paho.MQTT.Message(s);
message.destinationName = topic;
client.send(message);
如何下载javascript}
}, 1000 );
} function stop() {
window.ster);
}
Date.prototype.Format = function (fmt) { // author: meizz var o = { " M+ " : this .getMonth() + 1 , // ⽉份 " d+ " : this
.getDate(), // ⽇ " h+ " : this .getHours(), // ⼩时 " m+ " : this .getMinutes(), // 分 " s+ " : this .getSeconds(), // 秒 " q+ " : Math.floor(( this .getMonth() + 3 ) / 3 ), // 季度 " S " : this .getMilliseconds() // 毫秒 }; if ( / (y+) / .test(fmt)) fmt =
k]) : (( " 00 " + o[k]).substr(( "" + o[k]).length))); return fmt;
} script > head > < body > < input type ="text" id ="msg" /> < input type ="button" value ="Send" onclick ="send()" /> < input type ="button" value ="Start" onclick ="start()" /> < input type ="button" value ="Stop" onclick ="stop()" /> body > html >

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。