Sub:
var mqtt = require(‘mqtt’);
var opt = {
username:’KF’,
password:’Kfraspberry’
};
var client = mqtt.connect(‘mqtt://127.0.0.1’,opt);
client.on(‘connect’, function () {
console.log(‘connected’);
client.subscribe(‘KF_Client’)
})
client.on(‘message’, function (topic, message) {
if(message.toString()==”MQTT”){
console.log(‘AA’);
} else {
console.log(message.toString());
}
})
Pub:
var mqtt = require(‘mqtt’);
var opt = {
username:’KF’,
password:’Kfraspberry’
};
var client = mqtt.connect(‘mqtt://127.0.0.1’,opt);
client.publish(‘KF_Client’,’123′);
留言
要先安裝broker:https://mosquitto.org/download/