Commit beead226 by Chavee Issariyapat

resubscribe all active topics after remoteclient reconnect

parent 50e92fed
......@@ -16,13 +16,16 @@ const Coordinator = function(param={}) {
client.forward(topic, payload, options, topic, 0);
});
if (!that.localserver.sublist[deviceid]) {
if (that.localserver.sublist[deviceid] == undefined) {
that.remoteclient.subscribe(`->tap/shadow/${deviceid}:${client.token}`);
that.remoteclient.subscribe(`->tap/device/${deviceid}:${client.token}`);
that.localserver.sublist[deviceid] = 1;
that.localserver.sublist[deviceid] = {
count : 1,
token : client.token
}
}
else{
that.localserver.sublist[deviceid]++;
that.localserver.sublist[deviceid].count++;
}
});
......@@ -30,18 +33,29 @@ const Coordinator = function(param={}) {
console.log('client disconnected', client.id);
let deviceid = client.id.split(':')[0];
if (that.localserver.sublist[deviceid] == 1) {
if (that.localserver.sublist[deviceid].count <= 1) {
that.remoteclient.unsubscribe(`->tap/shadow/${deviceid}:${client.token}`);
that.remoteclient.unsubscribe(`->tap/device/${deviceid}:${client.token}`);
that.localserver.sublist[deviceid] = 0;
delete that.localserver.sublist[deviceid];
}
else {
that.localserver.sublist[deviceid]--;
that.localserver.sublist[deviceid].count--;
}
});
this.remoteclient.on('connect', function() {
setTimeout(function() {
for (let deviceid in that.localserver.sublist) {
//console.log(deviceid, that.localserver.sublist[deviceid])
that.remoteclient.subscribe(`->tap/shadow/${deviceid}:${that.localserver.sublist[deviceid].token}`);
that.remoteclient.subscribe(`->tap/device/${deviceid}:${that.localserver.sublist[deviceid].token}`);
}
}, 2000);
});
this.remoteclient.on('message', function(topic, payload){
//console.log(that.localserver.sublist);
console.log(that.localserver.sublist);
let jsonpayload = {};
try {
jsonpayload = JSON.parse(payload.toString());
......
......@@ -14,11 +14,13 @@ MQTTClient.prototype = new events.EventEmitter;
MQTTClient.prototype.connect = function() {
var that = this;
this.client = MQTT.connect(this.host, this.options || {});
this.client.on('connect', function() {
that.emit('connect');
});
this.client.on('disconnect', function() {
that.emit('disconnect');
});
this.client.on('message', async function(topic, payload) {
......@@ -58,7 +60,6 @@ MQTTClient.prototype.unsubscribe = function(topic) {
}
}
function create(param) {
return new MQTTClient(param);
}
......
......@@ -54,7 +54,7 @@ MQTTServer.prototype.publish = function(topic, payload) {
let msgobj = {
topic : topic,
payload : payload,
qos: 0,
qos: 2,
retain: false
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment