Commit aa9f8e03 by Chavee Issariyapat

update

parent 0df933eb
...@@ -20,6 +20,15 @@ const Coordinator = function(param={}) { ...@@ -20,6 +20,15 @@ const Coordinator = function(param={}) {
that.remoteclient.subscribe(`->tap/device/${deviceid}:${client.token}`); that.remoteclient.subscribe(`->tap/device/${deviceid}:${client.token}`);
}); });
this.localserver.on('clientDisconnected', function(client){
console.log('client disconnected', client.id);
let deviceid = client.id.split(':')[0];
that.remoteclient.unsubscribe(`->tap/shadow/${deviceid}:${client.token}`);
that.remoteclient.unsubscribe(`->tap/device/${deviceid}:${client.token}`);
});
this.remoteclient.on('message', function(topic, payload){ this.remoteclient.on('message', function(topic, payload){
let jsonpayload = {}; let jsonpayload = {};
try { try {
...@@ -27,6 +36,7 @@ const Coordinator = function(param={}) { ...@@ -27,6 +36,7 @@ const Coordinator = function(param={}) {
} }
catch(e) {} catch(e) {}
//console.log('incoming message ', topic, jsonpayload)
if (topic == '@shadow/data/updated') { if (topic == '@shadow/data/updated') {
that.localserver.publish(`${jsonpayload.deviceid}/shadow`, payload.toString()); that.localserver.publish(`${jsonpayload.deviceid}/shadow`, payload.toString());
} }
......
...@@ -37,7 +37,6 @@ MQTTClient.prototype.publish = function(topic, payload) { ...@@ -37,7 +37,6 @@ MQTTClient.prototype.publish = function(topic, payload) {
this.client.publish(topic, payload); this.client.publish(topic, payload);
} }
else { else {
} }
} }
...@@ -51,6 +50,7 @@ MQTTClient.prototype.subscribe = function(topic) { ...@@ -51,6 +50,7 @@ MQTTClient.prototype.subscribe = function(topic) {
} }
function create(param) { function create(param) {
return new MQTTClient(param); return new MQTTClient(param);
} }
......
...@@ -30,6 +30,10 @@ MQTTServer.prototype.start = function() { ...@@ -30,6 +30,10 @@ MQTTServer.prototype.start = function() {
that.emit('clientConnected',client); that.emit('clientConnected',client);
}); });
this.mqttserver.on('clientDisconnected', function(client, reason) {
that.emit('clientDisconnected',client, reason);
});
this.mqttserver.on('subscribed', function(topic, client) { this.mqttserver.on('subscribed', function(topic, client) {
that.emit('clientSubscribed',{ that.emit('clientSubscribed',{
deviceid: client.id, deviceid: client.id,
...@@ -42,7 +46,7 @@ MQTTServer.prototype.start = function() { ...@@ -42,7 +46,7 @@ MQTTServer.prototype.start = function() {
deviceid: client.id, deviceid: client.id,
topic: topic topic: topic
}); });
}); });
} }
MQTTServer.prototype.publish = function(topic, payload) { MQTTServer.prototype.publish = function(topic, payload) {
......
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