Commit beead226 by Chavee Issariyapat

resubscribe all active topics after remoteclient reconnect

parent 50e92fed
...@@ -16,13 +16,16 @@ const Coordinator = function(param={}) { ...@@ -16,13 +16,16 @@ const Coordinator = function(param={}) {
client.forward(topic, payload, options, topic, 0); 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/shadow/${deviceid}:${client.token}`);
that.remoteclient.subscribe(`->tap/device/${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{ else{
that.localserver.sublist[deviceid]++; that.localserver.sublist[deviceid].count++;
} }
}); });
...@@ -30,18 +33,29 @@ const Coordinator = function(param={}) { ...@@ -30,18 +33,29 @@ const Coordinator = function(param={}) {
console.log('client disconnected', client.id); console.log('client disconnected', client.id);
let deviceid = client.id.split(':')[0]; 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/shadow/${deviceid}:${client.token}`);
that.remoteclient.unsubscribe(`->tap/device/${deviceid}:${client.token}`); that.remoteclient.unsubscribe(`->tap/device/${deviceid}:${client.token}`);
that.localserver.sublist[deviceid] = 0; delete that.localserver.sublist[deviceid];
} }
else { 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){ this.remoteclient.on('message', function(topic, payload){
//console.log(that.localserver.sublist); console.log(that.localserver.sublist);
let jsonpayload = {}; let jsonpayload = {};
try { try {
jsonpayload = JSON.parse(payload.toString()); jsonpayload = JSON.parse(payload.toString());
......
...@@ -14,11 +14,13 @@ MQTTClient.prototype = new events.EventEmitter; ...@@ -14,11 +14,13 @@ MQTTClient.prototype = new events.EventEmitter;
MQTTClient.prototype.connect = function() { MQTTClient.prototype.connect = function() {
var that = this; var that = this;
this.client = MQTT.connect(this.host, this.options || {}); this.client = MQTT.connect(this.host, this.options || {});
this.client.on('connect', function() { this.client.on('connect', function() {
that.emit('connect'); that.emit('connect');
}); });
this.client.on('disconnect', function() { this.client.on('disconnect', function() {
that.emit('disconnect');
}); });
this.client.on('message', async function(topic, payload) { this.client.on('message', async function(topic, payload) {
...@@ -58,7 +60,6 @@ MQTTClient.prototype.unsubscribe = function(topic) { ...@@ -58,7 +60,6 @@ MQTTClient.prototype.unsubscribe = function(topic) {
} }
} }
function create(param) { function create(param) {
return new MQTTClient(param); return new MQTTClient(param);
} }
......
...@@ -54,7 +54,7 @@ MQTTServer.prototype.publish = function(topic, payload) { ...@@ -54,7 +54,7 @@ MQTTServer.prototype.publish = function(topic, payload) {
let msgobj = { let msgobj = {
topic : topic, topic : topic,
payload : payload, payload : payload,
qos: 0, qos: 2,
retain: false 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