This repository has been archived by the owner on Sep 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtinyMQTT.min.js
4 lines (4 loc) · 1.65 KB
/
tinyMQTT.min.js
1
2
3
4
(function(){function k(b){if(3===b.charCodeAt(0)>>4){var c=b.charCodeAt(2)<<8|b.charCodeAt(3);b={topic:b.substr(4,c),message:b.substr(4+c,b.charCodeAt(1)-c)};a.emit("message",b)}}function e(a){return f(a.length>>8,a.length&255)+a}function h(a,c,d){return f(a,c.length+d.length)+c+d}function l(b){var c=0;b=e(b);a.wt&&(c|=36,b+=e(a.wt)+e(a.wp));a.usr&&a.pwd&&(c|=a.usr?128:0,c|=a.usr&&a.pwd?64:0,b+=e(a.usr)+e(a.pwd));return h(16,e("MQTT")+f(4,c,a.ka>>8,a.ka&255),b)}var a,g=function(b,c){var d=c||{};this.svr=
b;this.prt=d.port||1883;this.ka=d.keep_alive||60;this.usr=d.username;this.pwd=d.password;this.cn=!1;this.ri=d.reconnect_interval||2E3;this.wt=d.will_topic;this.wp=d.will_payload||"";a=this},f=String.fromCharCode;g.prototype._scktClosed=function(){a.con&&(clearInterval(a.con),a.con=null);a.x1&&(clearInterval(a.x1),a.x1=null);a.cn=!1;delete a.cl;a.emit("disconnected")};g.prototype.connect=function(){var b=function(){a.con&&(clearInterval(a.con),a.con=null);try{a.cl.write(l(getSerial())),a.emit("connected"),
a.cn=!0,a.x1=setInterval(function(){a.cn&&a.cl.write(f(192,0))},a.ka<<10),a.cl.on("data",k.bind(a)),a.cl.on("end",a._scktClosed)}catch(c){a._scktClosed()}};a.cn||a.con||(a.con=setInterval(function(){a.cl&&(a.cl.end(),delete a.cl);try{a.cl=require("net").connect({host:a.svr,port:a.prt},b)}catch(c){a._scktClosed()}},a.ri))};g.prototype.subscribe=function(b){a.cl.write(h(130,f(256,1),e(b)+f(0)))};g.prototype.publish=function(b,c){if(127<b.length+c.length)throw"tMQTT-TL";a.cn&&(a.cl.write(h(49,e(b),c)),
a.emit("published"))};g.prototype.disconnect=function(){if(a.cn)try{a.cl.write(f(224,0))}catch(b){a._scktClosed()}};exports.create=function(a,c){return new g(a,c)}})();