From 4b9242d89d095f73d6eb89ef31eb24275c789c33 Mon Sep 17 00:00:00 2001 From: fzicaro Date: Mon, 5 Jun 2017 15:12:56 +0200 Subject: [PATCH 1/3] add unsubscribe function --- src/angular-MQTT.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/angular-MQTT.js b/src/angular-MQTT.js index 663cb50..416024c 100644 --- a/src/angular-MQTT.js +++ b/src/angular-MQTT.js @@ -29,7 +29,7 @@ angular.module('ngMQTT', []) try { var data = JSON.parse(payload.toString()); }catch (e){ - var data = payload.toString(); + throw new Error("received data can not parse for JSON !"); } angular.forEach(callbacks,function(callback, name){ var regexpStr = name.replace(new RegExp('(#)|(\\*)'),function(str){ @@ -41,7 +41,7 @@ angular.module('ngMQTT', []) }); if(topic.match(regexpStr)){ $rootScope.$apply(function() { - callback(data, topic); + callback(data); }); } }) @@ -56,5 +56,9 @@ angular.module('ngMQTT', []) Service.send = function(name, data){ client.publish(name, JSON.stringify(data)); }; + Service.drop = function(name, callback){ + callbacks[name] = callback; + client.unsubscribe(name); + }; return Service; }]); From 91f7abacfe16109fbe2e26694b00d75c47b2bb92 Mon Sep 17 00:00:00 2001 From: frazzaglia Date: Mon, 5 Jun 2017 18:25:47 +0200 Subject: [PATCH 2/3] Add management for plus character in topic string. --- src/angular-MQTT.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/angular-MQTT.js b/src/angular-MQTT.js index 416024c..5a07992 100644 --- a/src/angular-MQTT.js +++ b/src/angular-MQTT.js @@ -29,16 +29,24 @@ angular.module('ngMQTT', []) try { var data = JSON.parse(payload.toString()); }catch (e){ - throw new Error("received data can not parse for JSON !"); + var data = payload.toString(); } angular.forEach(callbacks,function(callback, name){ - var regexpStr = name.replace(new RegExp('(#)|(\\*)'),function(str){ - if(str=="#"){ - return ".*?" - }else if(str=="*"){ - return ".*?" - } - }); + var regexpStr = name.replace(new RegExp('(#)|(\\*)|(\\+)'),function(str){ + switch (str) { + case "#": + return ".*?" + break; + case "*": + return ".*?" + break; + case "+": + return ".*" + break; + default: + break; + } + }); if(topic.match(regexpStr)){ $rootScope.$apply(function() { callback(data); From d0fd8930aab9a596d94c79ba55af6693254e3b0a Mon Sep 17 00:00:00 2001 From: fzicaro Date: Tue, 6 Jun 2017 17:10:18 +0200 Subject: [PATCH 3/3] add topic return in subscribe callback --- src/angular-MQTT.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/angular-MQTT.js b/src/angular-MQTT.js index 5a07992..d1ca520 100644 --- a/src/angular-MQTT.js +++ b/src/angular-MQTT.js @@ -49,7 +49,7 @@ angular.module('ngMQTT', []) }); if(topic.match(regexpStr)){ $rootScope.$apply(function() { - callback(data); + callback(data, topic); }); } })