0
我在嘗試使用mqtt.js和Mosca來發送基於author's demo和other instructions的離線消息。下面是我嘗試的,但我不知道爲什麼我的偵聽客戶端在線工作,但不知道訂閱過程包括脫機配置(QoS,clientId,clean)時。使用mqtt.js和Mosca發出的離線消息的問題
1.開始使用獨立莫斯卡經紀人:
npm install mosca bunyan -g
mosca -v | bunyan
2.Run以下腳本(見下表)順序:
node subscribe.js // User8 subscribes to topic called Channel-01 with QoS=1, then closes connection
node send.js // TxUser sends a message on Channel-01
node listen.js // User8 reconnects and should see TxUser's message
3.Attempt找出爲什麼listen.js
沒有收到TxUser的信息。
這裏是我的腳本:
subscribe.js User8訂閱名爲Channel-01
與QoS=1
話題,然後關閉連接。
var mqtt = require('mqtt');
var client = mqtt.connect({
servers: [{ host: 'localhost', port: 1883 }]
, clientId:"User8"
, clean:false
});
client.subscribe('Channel-01', {qos:1} , function(){
console.log("Subscriber Client: subscribed and closing connection.");
client.end();
});
send.js TxUser發送上通道01
var mqtt = require('mqtt');
var client = mqtt.connect({
servers: [{ host: 'localhost', port: 1883 }]
, clientId:"TxUser"
, clean:false
});
client.on('connect', function(){
client.publish('Channel-01', '* * * IMPORTANT msg ' + Date() + ' * * *' , function() {
client.end(function(){
console.log('Sender Client: published message and closed connection');
});
});
});
listen.js User8重新連接的消息和應該看到TxUser的消息
var mqtt = require('mqtt');
var client = mqtt.connect({
servers: [{ host: 'localhost', port: 1883 }]
, clientId:"User8"
, clean:false
});
client.subscribe('Channel-01');
client.on('message', function(topic, message) {
// this is never fired when offline options (QoS, clientId, clean)
// are configured in subscribe.js
console.log('Listener Client: Message Received = ',message.toString());
});
setTimeout(function() {
console.log('Listener Client: Exiting');
client.end();
},10*1000);
包.js
{
"name": "MQTT-Test-System",
"version": "0.0.1",
"dependencies": {
"mosca": "1.0.1",
"mqtt": "1.6.3"
}
}