text: function f(b,a){this.server=b;a=a||{};this.port=a.port||q.DEF_PORT;this.client_id=a.client_id||v();this.keep_alive=a.keep_alive||q.DEF_KEEP_ALIVE;this.clean_session=a.clean_session||!0;this.username=a.username;this.password=a.password;this.connected=this.client=!1;this.ping_interval=this.keep_alive<this.C.PING_INTERVAL?this.keep_alive-5:this.C.PING_INTERVAL;this.protocol_name=a.protocol_name||"MQTT";this.protocol_level=h(parseInt((a.protocol_level||q.PROTOCOL_LEVEL).toString(16),16))}
  function l(b){return h(b.length>>8,b.length&255)+b}function n(b,a,e){b=h(b);var d=a.length+e.length,c="";do{var t=d&127;d>>=7;0<d&&(t+=128);c+=h(t)}while(0<d);return b+c+a+e}function r(){m=65534<m?1:++m;return h(m>>8)+h(m&255)}function w(b,a,p,d){d|=e.PUBLISH<<4|p<<1;b=l(b);0<p&&(b+=r());return n(d,b,a)}function x(b,a){return n(e.SUBSCRIBE<<4|2,r(),l(b)+h(a))}function y(b){return n(e.UNSUBSCRIBE<<4|2,r(),l(b))}var q={PROTOCOL_LEVEL:4,DEF_PORT:1883,DEF_KEEP_ALIVE:60},e={CONNECT:1,CONNACK:2,PUBLISH:3,
  PUBACK:4,PUBREC:5,PUBREL:6,PUBCOMP:7,SUBSCRIBE:8,SUBACK:9,UNSUBSCRIBE:10,UNSUBACK:11,PINGREQ:12,PINGRESP:13,DISCONNECT:14},m=Math.floor(65534*Math.random()),u={0:"ACCEPTED",1:"UNACCEPTABLE_PROTOCOL_VERSION",2:"IDENTIFIER_REJECTED",3:"SERVER_UNAVAILABLE",4:"BAD_USER_NAME_OR_PASSWORD",5:"NOT_AUTHORIZED"};f.prototype.C={DEF_QOS:0,CONNECT_TIMEOUT:1E4,PING_INTERVAL:40};var h=String.fromCharCode,v=function(){function b(){return Math.floor(65536*(1+Math.random())).toString(16).substring(1)}return function(){return b()+
  b()+b()}}();f.prototype.connect=function(b){var a=this,p=function(){a.pintr&&clearInterval(a.pintr);a.pintr=setInterval(function(){a.ping()},1E3*a.ping_interval)},d=function(){b.write(a.mqttConnect(a.client_id));a.ctimo=setTimeout(function(){a.ctimo=void 0;a.emit("disconnected");a.disconnect()},a.C.CONNECT_TIMEOUT);b.on("data",function(c){a.partData&&(c=a.partData+c,a.partData="");var d=c.charCodeAt(0)>>4;var f=c.substr(1,5);var k=0,g=0;do{var l=f.charCodeAt(k);g|=(l&127)<<7*k++}while(l&128&&4>k);
  f=k;k=g+f+1;c.length<k?a.partData=c:(g=c.substr(f+1,k),c.length>k&&b.emit("data",c.substr(k)),d===e.PUBLISH?(c=c[0],2<=g.length&&"undefined"!==typeof g?(d=(c&6)>>1,f=g.charCodeAt(0)<<8|g.charCodeAt(1),k=2+f+(d?2:0),c={topic:g.substr(2,f),message:g.substr(k,g.length-k),dup:(c&8)>>3,qos:d,pid:d?g.substr(2+f,2):0,retain:c&1}):c=void 0,void 0!==c&&(c.qos&&b.write(h((1==c.qos?e.PUBACK:e.PUBREC)<<4)+"\u0002"+c.pid),a.emit("publish",c),a.emit("message",c.topic,c.message))):d===e.PUBACK?a.emit("puback",c.charCodeAt(2)<<
  8|c.charCodeAt(3)):d===e.PUBREC?b.write(h(e.PUBREL<<4|2)+"\u0002"+g.substr(0,2)):d===e.PUBREL?b.write(h(e.PUBCOMP<<4)+"\u0002"+g.substr(0,2)):d===e.PUBCOMP?a.emit("pubcomp",c.charCodeAt(2)<<8|c.charCodeAt(3)):d===e.SUBACK?0<g.length&&(128==g[g.length-1]?a.emit("subscribed_fail"):a.emit("subscribed")):d===e.UNSUBACK?a.emit("unsubscribed"):d===e.PINGREQ?b.write(h(e.PINGRESP<<4)+"\x00"):d===e.PINGRESP?a.emit("ping_reply"):d===e.CONNACK?(a.ctimo&&clearTimeout(a.ctimo),a.ctimo=void 0,a.partData="",c=g.charCodeAt(3),
  "ACCEPTED"===u[c]?(a.connected=!0,p(),a.emit("connected"),a.emit("connect")):(d="Connection refused, ",a.connected=!1,d=0<c&&6>c?d+u[c]:d+("unknown return code: "+c+"."),a.emit("error",d))):a.emit("error","MQTT unsupported packet type: "+d))});b.on("end",function(){a._scktClosed()});a.client=b};if(b)d();else try{b=require("net").connect({host:a.server,port:a.port},d)}catch(c){this.client=!1,this.emit("error",c.message),this.emit("disconnected")}};f.prototype._scktClosed=function(){this.connected&&
  (this.client=this.connected=!1,this.pintr&&clearInterval(this.pintr),this.ctimo&&clearTimeout(this.ctimo),this.pintr=this.ctimo=void 0,this.emit("disconnected"),this.emit("close"))};f.prototype.disconnect=function(){if(this.client){try{this.client.write(h(e.DISCONNECT<<4)+"\x00")}catch(b){return this._scktClosed()}this.client.end();this.client=!1}};f.prototype.publish=function(b,a,e){if(this.client){e=e||{};try{this.client.write(w(b,a.toString(),e.qos||this.C.DEF_QOS,(e.retain?1:0)|(e.dup?8:0)))}catch(d){this._scktClosed()}}};
  f.prototype.subscribe=function(b,a){if(this.client){a=a||{};var e=[];"string"===typeof b&&(b=[b]);Array.isArray(b)?b.forEach(function(b){e.push({topic:b,qos:a.qos||this.C.DEF_QOS})}.bind(this)):Object.keys(b).forEach(function(a){e.push({topic:a,qos:b[a]})});e.forEach(function(a){this.client.write(x(a.topic,a.qos))}.bind(this))}};f.prototype.unsubscribe=function(b){this.client&&this.client.write(y(b))};f.prototype.ping=function(){if(this.client)try{this.client.write(h(e.PINGREQ<<4)+"\x00")}catch(b){this._scktClosed()}};
  f.prototype.createFlagsForConnection=function(b){var a=0|(this.username?128:0);a|=this.username&&this.password?64:0;a|=b.clean_session?2:0;return h(parseInt(a.toString(16),16))};f.prototype.mqttConnect=function(b){var a=e.CONNECT<<4;b=this.createFlagsForConnection({clean_session:b});var f=h(this.keep_alive>>8,this.keep_alive&255),d=l(this.client_id);this.username&&(d+=l(this.username),this.password&&(d+=l(this.password)));return n(a,l(this.protocol_name)+this.protocol_level+b+f,d)};exports.create=
  function(b,a){return new f(b,a)};exports.connect=function(b){b=new f(b.host,b);b.connect();return b}