最近看了一下,mqtt,简单的做一下总结。
先说使用场景,mqtt 适用于网络条件极端环境下使用,对网络要求较低。比如物联网。另外也有一下比较高的可靠性,比如最少有一次到达。
实现:mqtt是使用字节来实现,有什么报文什么的,看了一头雾水。比较方便的是,有一些第三方工具,比如rabbitmq 提供的mqtt插件,还有前端的mqtt.js,各种工具,使用起来和其他的mq没什么区别。
问题:如果同时订阅两个topic,监听消息用几次
一下代码也是从别人那抄来的,运行了一下,没问题。需要用到nodejs
1、基础组件
npm install koa --save
npm install mqtt --save
npm install jsdom --save
npm install mosac --save
生产端
var mqtt = require('mqtt');
var client = mqtt.connect('mqtt://localhost:1883');
client.on('connect', function () {
console.log('>>> connected')
// client.subscribe('/tips')
setInterval(
()=>{
var t= Math.floor(Math.random()*100);
client.publish('/temperature',t.toString());
},
3000
);
})
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString())
})
node js mq 组件
var mosca = require('mosca');
var ascoltatore = {
//using ascoltatore
// type: 'mongo',
// url: 'mongodb://localhost:27017/mqtt',
// pubsubCollection: 'ascoltatori',
// mongo: {}
};
var settings = {
port: 1883,
backend: ascoltatore
};
var server = new mosca.Server(settings);
server.on('clientConnected', function(client) {
console.log('client connected', client.id);
});
// fired when a message is received
server.on('published', function(packet, client) {
console.log('Published', packet.payload); //{"clientId":"mqttjs_02fea7b4","topic":"/tips"}
// console.log('>>>packet', packet); //{"clientId":"mqttjs_02fea7b4","topic":"/tips"}
});
server.on('ready', setup);
// fired when the mqtt server is ready
function setup() {
console.log('Mosca server is up and running');
}
消费端 :
'use strict'
const mqtt = require('mqtt');
var client = mqtt.connect('mqtt://localhost:1883');
client.on('connect', function () {
console.log('>>> connected');
client.subscribe('/temperature');
})
client.on('message', function (topic, message) {
var temperature = parseInt(message.toString());
var data = {temperature};
if (temperature >= 60) {
data.tips = "热... 500服务器故障111";
}
else if (temperature >= 50) {
data.tips = "今天天气非常热,建议不要穿衣服了";
}
else if (temperature >= 40) {
data.tips = "今天天气十分的热,建议穿短袖T恤+短裤";
}
else if (temperature >= 30) {
data.tips = "今天天气有点的热,建议穿短袖T恤";
}
else if (temperature >= 0) {
data.tips = "今天天气正好,可以穿上一件薄衣服";
}
else if (temperature >= -10) {
data.tips = "今天天气十分寒冷,棉袄可以穿上一件";
}
else {
data.tips = "今天天气十分十分寒冷,棉袄可以穿上二件";
}
client.publish('/tips', JSON.stringify(data));
// if (temperature+1) {}
// message is Buffer
console.log(JSON.stringify(data));
})
在页面上展示
'use strict'
const Koa = require('koa');
const mqtt = require('mqtt');
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window } = new JSDOM(``);
var $ = require('jQuery')(window);
const app = new Koa();
var msg = {temperature:"-",tips:""};
// response
app.use(ctx => {
ctx.body = "
当前温度:" + msg.temperature + "度" + "\n" + '穿衣提示:'+msg.tips + "\n
" ;
});
app.listen(3000);
//mqtt
var client = mqtt.connect('mqtt://localhost:1883');
client.on('connect', function () {
console.log('>>> connected');
client.subscribe('/tips');
})
client.on('message', function (topic, message) {
var data = JSON.parse(message.toString());
console.log(message.toString());
console.log(data.tips);
console.log(topic);
msg = data;
// if (temperature+1) {}
// message is Buffer
// let str = message.toString();
// let data = JSON.parse(message);
// console.log(data.tips);
// msg = message.toString();
})