miniprogram-mqtt
Version:
*** ##### 初始化 ```javascript //如使用了babel编译至commonjs规范(wepy框架自带) import Mqtt from 'miniprogram-mqtt' const mqtt = new Mqtt({ //必填参数 host: 'host', //mqtt服务器 port: null, //端口 userName: '', //账户名 password: '' //密码
1 lines • 37.2 kB
JavaScript
module.exports=function(e){var t={};function n(s){if(t[s])return t[s].exports;var i=t[s]={exports:{},id:s,loaded:!1};return e[s].call(i.exports,i,i.exports,n),i.loaded=!0,i.exports}return n.m=e,n.c=t,n.p="",n(0)}([function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var s,i=n(1),o=(s=i)&&s.__esModule?s:{default:s};t.default=o.default,e.exports=t.default},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var s=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var s in n)Object.prototype.hasOwnProperty.call(n,s)&&(e[s]=n[s])}return e},i=function(){function e(e,t){for(var n=0;n<t.length;n++){var s=t[n];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(e,s.key,s)}}return function(t,n,s){return n&&e(t.prototype,n),s&&e(t,s),t}}(),o=a(n(2)),r=a(n(4));function a(e){return e&&e.__esModule?e:{default:e}}var c=function(){function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.globalData={handler:null,userInfo:null,client:null,onMessageArrived:{},subscribed:{}},this.mqttConfig=t}return i(e,[{key:"subscribe",value:function(e,t){var n=this,s=this.globalData.client;if(t.onFailure||(t.onFailure=function(s){console.log(s),n.subscribe(e,t)}),s&&s.isConnected())return console.log("subscribe success: "+e),this.globalData.subscribed[e]=JSON.stringify(t),s.subscribe(e,t)}},{key:"publish",value:function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0,s=arguments.length>3&&void 0!==arguments[3]&&arguments[3],i=this.globalData.client;if(i&&i.isConnected()){var r=new o.default.Message(t);return r.destinationName=e,r.qos=n,r.retained=s,i.send(r)}}},{key:"resub",value:function(){for(var e in this.globalData.subscribed)this.subscribe(e,JSON.parse(this.globalData.subscribed[e]))}},{key:"setOnMessageArrived",value:function(e,t){"function"==typeof t&&(this.globalData.onMessageArrived[e]=t)}},{key:"setOnConnectionLost",value:function(e){"function"==typeof e&&(this.globalData.onConnectionLost=e)}},{key:"__onMessageArrive",value:function(e){var t=this;e.onMessageArrived=function(e){if("function"==typeof t.globalData.onMessageArrived[e.topic])try{return t.globalData.onMessageArrived[e.topic](e)}catch(e){console.log(e)}}}},{key:"__onConnectionLost",value:function(e){var t=this;e.onConnectionLost=function(e){if("function"==typeof t.globalData.onConnectionLost)return t.globalData.onConnectionLost(e);0!==e.errorCode&&console.log("onConnectionLost:"+e.errorMessage)}}},{key:"init",value:function(){var e=this;return new Promise(function(t,n){var i=new o.default.Client(e.mqttConfig.host,e.mqttConfig.port,(0,r.default)());delete e.mqttConfig.host,delete e.mqttConfig.port;var a={useSSL:!0,cleanSession:!0,keepAliveInterval:200,reconnect:!0};(a=s({},a,e.mqttConfig)).onSuccess=function(){console.log("connected"),e.globalData.client=i,e.resub(),e.__onMessageArrive(i),e.__onConnectionLost(i),t()},a.onFailure=function(e){console.log(e),n(err)},i.connect(a)})}}]),e}();t.default=c,e.exports=t.default},function(e,t,n){var s,i;(function(e,o){"use strict";var r,a="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};r=function(){return function(e){var t={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},n=function(e,t){for(var n in e)if(e.hasOwnProperty(n)){if(!t.hasOwnProperty(n)){var s="Unknown property, "+n+". Valid properties are:";for(var o in t)t.hasOwnProperty(o)&&(s=s+" "+o);throw new Error(s)}if(a(e[n])!==t[n])throw new Error(r(i.INVALID_TYPE,[a(e[n]),n]))}},s=function(e,t){return function(){return e.apply(t,arguments)}},i={OK:{code:0,text:"AMQJSC0000I OK."},CONNECT_TIMEOUT:{code:1,text:"AMQJSC0001E Connect timed out."},SUBSCRIBE_TIMEOUT:{code:2,text:"AMQJS0002E Subscribe timed out."},UNSUBSCRIBE_TIMEOUT:{code:3,text:"AMQJS0003E Unsubscribe timed out."},PING_TIMEOUT:{code:4,text:"AMQJS0004E Ping timed out."},INTERNAL_ERROR:{code:5,text:"AMQJS0005E Internal error. Error Message: {0}, Stack trace: {1}"},CONNACK_RETURNCODE:{code:6,text:"AMQJS0006E Bad Connack return code:{0} {1}."},SOCKET_ERROR:{code:7,text:"AMQJS0007E Socket error:{0}."},SOCKET_CLOSE:{code:8,text:"AMQJS0008I Socket closed."},MALFORMED_UTF:{code:9,text:"AMQJS0009E Malformed UTF data:{0} {1} {2}."},UNSUPPORTED:{code:10,text:"AMQJS0010E {0} is not supported by this browser."},INVALID_STATE:{code:11,text:"AMQJS0011E Invalid state {0}."},INVALID_TYPE:{code:12,text:"AMQJS0012E Invalid type {0} for {1}."},INVALID_ARGUMENT:{code:13,text:"AMQJS0013E Invalid argument {0} for {1}."},UNSUPPORTED_OPERATION:{code:14,text:"AMQJS0014E Unsupported operation."},INVALID_STORED_DATA:{code:15,text:"AMQJS0015E Invalid data in local storage key={0} value={1}."},INVALID_MQTT_MESSAGE_TYPE:{code:16,text:"AMQJS0016E Invalid MQTT message type {0}."},MALFORMED_UNICODE:{code:17,text:"AMQJS0017E Malformed Unicode string:{0} {1}."},BUFFER_FULL:{code:18,text:"AMQJS0018E Message buffer is full, maximum buffer size: {0}."}},o={0:"Connection Accepted",1:"Connection Refused: unacceptable protocol version",2:"Connection Refused: identifier rejected",3:"Connection Refused: server unavailable",4:"Connection Refused: bad user name or password",5:"Connection Refused: not authorized"},r=function(e,t){var n=e.text;if(t)for(var s,i,o=0;o<t.length;o++)if(s="{"+o+"}",(i=n.indexOf(s))>0){var r=n.substring(0,i),a=n.substring(i+s.length);n=r+t[o]+a}return n},c=[0,6,77,81,73,115,100,112,3],h=[0,4,77,81,84,84,4],u=function(e,t){for(var n in this.type=e,t)t.hasOwnProperty(n)&&(this[n]=t[n])};function d(e,n){var s,i=n,o=e[n],r=o>>4,a=o&=15;n+=1;var c=0,h=1;do{if(n==e.length)return[null,i];c+=(127&(s=e[n++]))*h,h*=128}while(0!=(128&s));var d=n+c;if(d>e.length)return[null,i];var l=new u(r);switch(r){case t.CONNACK:1&e[n++]&&(l.sessionPresent=!0),l.returnCode=e[n++];break;case t.PUBLISH:var f=a>>1&3,g=_(e,n),p=v(e,n+=2,g);n+=g,f>0&&(l.messageIdentifier=_(e,n),n+=2);var y=new M(e.subarray(n,d));1==(1&a)&&(y.retained=!0),8==(8&a)&&(y.duplicate=!0),y.qos=f,y.destinationName=p,l.payloadMessage=y;break;case t.PUBACK:case t.PUBREC:case t.PUBREL:case t.PUBCOMP:case t.UNSUBACK:l.messageIdentifier=_(e,n);break;case t.SUBACK:l.messageIdentifier=_(e,n),n+=2,l.returnCode=e.subarray(n,d)}return[l,d]}function l(e,t,n){return t[n++]=e>>8,t[n++]=e%256,n}function f(e,t,n,s){return p(e,n,s=l(t,n,s)),s+t}function _(e,t){return 256*e[t]+e[t+1]}function g(e){for(var t=0,n=0;n<e.length;n++){var s=e.charCodeAt(n);s>2047?(55296<=s&&s<=56319&&(n++,t++),t+=3):s>127?t+=2:t++}return t}function p(e,t,n){for(var s=n,o=0;o<e.length;o++){var a=e.charCodeAt(o);if(55296<=a&&a<=56319){var c=e.charCodeAt(++o);if(isNaN(c))throw new Error(r(i.MALFORMED_UNICODE,[a,c]));a=c-56320+(a-55296<<10)+65536}a<=127?t[s++]=a:a<=2047?(t[s++]=a>>6&31|192,t[s++]=63&a|128):a<=65535?(t[s++]=a>>12&15|224,t[s++]=a>>6&63|128,t[s++]=63&a|128):(t[s++]=a>>18&7|240,t[s++]=a>>12&63|128,t[s++]=a>>6&63|128,t[s++]=63&a|128)}return t}function v(e,t,n){for(var s,o="",a=t;a<t+n;){var c=e[a++];if(c<128)s=c;else{var h=e[a++]-128;if(h<0)throw new Error(r(i.MALFORMED_UTF,[c.toString(16),h.toString(16),""]));if(c<224)s=64*(c-192)+h;else{var u=e[a++]-128;if(u<0)throw new Error(r(i.MALFORMED_UTF,[c.toString(16),h.toString(16),u.toString(16)]));if(c<240)s=4096*(c-224)+64*h+u;else{var d=e[a++]-128;if(d<0)throw new Error(r(i.MALFORMED_UTF,[c.toString(16),h.toString(16),u.toString(16),d.toString(16)]));if(!(c<248))throw new Error(r(i.MALFORMED_UTF,[c.toString(16),h.toString(16),u.toString(16),d.toString(16)]));s=262144*(c-240)+4096*h+64*u+d}}}s>65535&&(s-=65536,o+=String.fromCharCode(55296+(s>>10)),s=56320+(1023&s)),o+=String.fromCharCode(s)}return o}u.prototype.encode=function(){var e,n=(15&this.type)<<4,s=0,i=[],o=0;switch(void 0!==this.messageIdentifier&&(s+=2),this.type){case t.CONNECT:switch(this.mqttVersion){case 3:s+=c.length+3;break;case 4:s+=h.length+3}s+=g(this.clientId)+2,void 0!==this.willMessage&&(s+=g(this.willMessage.destinationName)+2,(e=this.willMessage.payloadBytes)instanceof Uint8Array||(e=new Uint8Array(a)),s+=e.byteLength+2),void 0!==this.userName&&(s+=g(this.userName)+2),void 0!==this.password&&(s+=g(this.password)+2);break;case t.SUBSCRIBE:n|=2;for(var r=0;r<this.topics.length;r++)i[r]=g(this.topics[r]),s+=i[r]+2;s+=this.requestedQos.length;break;case t.UNSUBSCRIBE:n|=2;for(r=0;r<this.topics.length;r++)i[r]=g(this.topics[r]),s+=i[r]+2;break;case t.PUBREL:n|=2;break;case t.PUBLISH:this.payloadMessage.duplicate&&(n|=8),n=n|=this.payloadMessage.qos<<1,this.payloadMessage.retained&&(n|=1),s+=(o=g(this.payloadMessage.destinationName))+2;var a=this.payloadMessage.payloadBytes;s+=a.byteLength,a instanceof ArrayBuffer?a=new Uint8Array(a):a instanceof Uint8Array||(a=new Uint8Array(a.buffer));break;case t.DISCONNECT:}var u=function(e){var t=new Array(1),n=0;do{var s=e%128;(e>>=7)>0&&(s|=128),t[n++]=s}while(e>0&&n<4);return t}(s),d=u.length+1,_=new ArrayBuffer(s+d),p=new Uint8Array(_);if(p[0]=n,p.set(u,1),this.type==t.PUBLISH)d=f(this.payloadMessage.destinationName,o,p,d);else if(this.type==t.CONNECT){switch(this.mqttVersion){case 3:p.set(c,d),d+=c.length;break;case 4:p.set(h,d),d+=h.length}var v=0;this.cleanSession&&(v=2),void 0!==this.willMessage&&(v|=4,v|=this.willMessage.qos<<3,this.willMessage.retained&&(v|=32)),void 0!==this.userName&&(v|=128),void 0!==this.password&&(v|=64),p[d++]=v,d=l(this.keepAliveInterval,p,d)}switch(void 0!==this.messageIdentifier&&(d=l(this.messageIdentifier,p,d)),this.type){case t.CONNECT:d=f(this.clientId,g(this.clientId),p,d),void 0!==this.willMessage&&(d=f(this.willMessage.destinationName,g(this.willMessage.destinationName),p,d),d=l(e.byteLength,p,d),p.set(e,d),d+=e.byteLength),void 0!==this.userName&&(d=f(this.userName,g(this.userName),p,d)),void 0!==this.password&&(d=f(this.password,g(this.password),p,d));break;case t.PUBLISH:p.set(a,d);break;case t.SUBSCRIBE:for(r=0;r<this.topics.length;r++)d=f(this.topics[r],i[r],p,d),p[d++]=this.requestedQos[r];break;case t.UNSUBSCRIBE:for(r=0;r<this.topics.length;r++)d=f(this.topics[r],i[r],p,d)}return _};var y=function(n,s){this._client=n,this._keepAliveInterval=1e3*s,this.isReset=!1;var o=new u(t.PINGREQ).encode(),a=function(e){return function(){return c.apply(e)}},c=function(){this.isReset?(this.isReset=!1,this._client._trace("Pinger.doPing","send PINGREQ"),e.sendSocketMessage({data:o,success:function(){},fail:function(){},complete:function(){}}),this.timeout=setTimeout(a(this),this._keepAliveInterval)):(this._client._trace("Pinger.doPing","Timed out"),this._client._disconnected(i.PING_TIMEOUT.code,r(i.PING_TIMEOUT)))};this.reset=function(){this.isReset=!0,clearTimeout(this.timeout),this._keepAliveInterval>0&&(this.timeout=setTimeout(a(this),this._keepAliveInterval))},this.cancel=function(){clearTimeout(this.timeout)}},I=function(e,t,n,s){t||(t=30);this.timeout=setTimeout(function(e,t,n){return function(){return e.apply(t,n)}}(n,e,s),1e3*t),this.cancel=function(){clearTimeout(this.timeout)}},m=function(t,n,s,i,o){for(var r in this._trace("Paho.MQTT.Client",t,n,s,i,o),this.host=n,this.port=s,this.path=i,this.uri=t,this.clientId=o,this._wsuri=null,this._localKey=n+":"+s+("/mqtt"!=i?":"+i:"")+":"+o+":",this._msg_queue=[],this._buffered_msg_queue=[],this._sentMessages={},this._receivedMessages={},this._notify_msg_sent={},this._message_identifier=1,this._sequence=0,e.getStorageInfoSync().keys)0!==r.indexOf("Sent:"+this._localKey)&&0!==r.indexOf("Received:"+this._localKey)||this.restore(r)};m.prototype.host=null,m.prototype.port=null,m.prototype.path=null,m.prototype.uri=null,m.prototype.clientId=null,m.prototype.socket=null,m.prototype.connected=!1,m.prototype.maxMessageIdentifier=65536,m.prototype.connectOptions=null,m.prototype.hostIndex=null,m.prototype.onConnected=null,m.prototype.onConnectionLost=null,m.prototype.onMessageDelivered=null,m.prototype.onMessageArrived=null,m.prototype.traceFunction=null,m.prototype._msg_queue=null,m.prototype._buffered_msg_queue=null,m.prototype._connectTimeout=null,m.prototype.sendPinger=null,m.prototype.receivePinger=null,m.prototype._reconnectInterval=1,m.prototype._reconnecting=!1,m.prototype._reconnectTimeout=null,m.prototype.disconnectedPublishing=!1,m.prototype.disconnectedBufferSize=5e3,m.prototype.receiveBuffer=null,m.prototype._traceBuffer=null,m.prototype._MAX_TRACE_ENTRIES=100,m.prototype.connect=function(e){var t=this._traceMask(e,"password");if(this._trace("Client.connect",t,null,this.connected),this.connected)throw new Error(r(i.INVALID_STATE,["already connected"]));this._reconnecting&&(this._reconnectTimeout.cancel(),this._reconnectTimeout=null,this._reconnecting=!1),this.connectOptions=e,this._reconnectInterval=1,this._reconnecting=!1,e.uris?(this.hostIndex=0,this._doConnect(e.uris[0])):this._doConnect(this.uri)},m.prototype.subscribe=function(e,n){if(this._trace("Client.subscribe",e,n),!this.connected)throw new Error(r(i.INVALID_STATE,["not connected"]));var s=new u(t.SUBSCRIBE);s.topics=[e],void 0!==n.qos?s.requestedQos=[n.qos]:s.requestedQos=[0],n.onSuccess&&(s.onSuccess=function(e){n.onSuccess({invocationContext:n.invocationContext,grantedQos:e})}),n.onFailure&&(s.onFailure=function(e){n.onFailure({invocationContext:n.invocationContext,errorCode:e,errorMessage:r(e)})}),n.timeout&&(s.timeOut=new I(this,n.timeout,n.onFailure,[{invocationContext:n.invocationContext,errorCode:i.SUBSCRIBE_TIMEOUT.code,errorMessage:r(i.SUBSCRIBE_TIMEOUT)}])),this._requires_ack(s),this._schedule_message(s)},m.prototype.unsubscribe=function(e,n){if(this._trace("Client.unsubscribe",e,n),!this.connected)throw new Error(r(i.INVALID_STATE,["not connected"]));var s=new u(t.UNSUBSCRIBE);s.topics=[e],n.onSuccess&&(s.callback=function(){n.onSuccess({invocationContext:n.invocationContext})}),n.timeout&&(s.timeOut=new I(this,n.timeout,n.onFailure,[{invocationContext:n.invocationContext,errorCode:i.UNSUBSCRIBE_TIMEOUT.code,errorMessage:r(i.UNSUBSCRIBE_TIMEOUT)}])),this._requires_ack(s),this._schedule_message(s)},m.prototype.send=function(e){this._trace("Client.send",e);var n=new u(t.PUBLISH);if(n.payloadMessage=e,this.connected)e.qos>0?this._requires_ack(n):this.onMessageDelivered&&(this._notify_msg_sent[n]=this.onMessageDelivered(n.payloadMessage)),this._schedule_message(n);else{if(!this._reconnecting||!this.disconnectedPublishing)throw new Error(r(i.INVALID_STATE,["not connected"]));if(Object.keys(this._sentMessages).length+this._buffered_msg_queue.length>this.disconnectedBufferSize)throw new Error(r(i.BUFFER_FULL,[this.disconnectedBufferSize]));e.qos>0?this._requires_ack(n):(n.sequence=++this._sequence,this._buffered_msg_queue.push(n))}},m.prototype.disconnect=function(){if(this._trace("Client.disconnect"),this._reconnecting&&(this._reconnectTimeout.cancel(),this._reconnectTimeout=null,this._reconnecting=!1),!this.connected)throw new Error(r(i.INVALID_STATE,["not connecting or connected"]));var e=new u(t.DISCONNECT);this._notify_msg_sent[e]=s(this._disconnected,this),this._schedule_message(e)},m.prototype.getTraceLog=function(){if(null!==this._traceBuffer){for(var e in this._trace("Client.getTraceLog",new Date),this._trace("Client.getTraceLog in flight messages",this._sentMessages.length),this._sentMessages)this._trace("_sentMessages ",e,this._sentMessages[e]);for(var e in this._receivedMessages)this._trace("_receivedMessages ",e,this._receivedMessages[e]);return this._traceBuffer}},m.prototype.startTrace=function(){null===this._traceBuffer&&(this._traceBuffer=[]),this._trace("Client.startTrace",new Date,"@VERSION@")},m.prototype.stopTrace=function(){delete this._traceBuffer},m.prototype._doConnect=function(t){if(this.connectOptions.useSSL){var n=t.split(":");n[0]="wss",t=n.join(":")}this._wsuri=t,this.connected=!1,e.connectSocket({url:t}),e.onSocketOpen(s(this._on_socket_open,this)),e.onSocketMessage(s(this._on_socket_message,this)),e.onSocketError(s(this._on_socket_error,this)),e.onSocketClose(s(this._on_socket_close,this)),this.sendPinger=new y(this,this.connectOptions.keepAliveInterval),this.receivePinger=new y(this,this.connectOptions.keepAliveInterval),this._connectTimeout&&(this._connectTimeout.cancel(),this._connectTimeout=null),this._connectTimeout=new I(this,this.connectOptions.timeout,this._disconnected,[i.CONNECT_TIMEOUT.code,r(i.CONNECT_TIMEOUT)])},m.prototype._schedule_message=function(e){this._msg_queue.push(e),this.connected&&this._process_queue()},m.prototype.store=function(n,s){var o={type:s.type,messageIdentifier:s.messageIdentifier,version:1};switch(s.type){case t.PUBLISH:s.pubRecReceived&&(o.pubRecReceived=!0),o.payloadMessage={};for(var a="",c=s.payloadMessage.payloadBytes,h=0;h<c.length;h++)c[h]<=15?a=a+"0"+c[h].toString(16):a+=c[h].toString(16);o.payloadMessage.payloadHex=a,o.payloadMessage.qos=s.payloadMessage.qos,o.payloadMessage.destinationName=s.payloadMessage.destinationName,s.payloadMessage.duplicate&&(o.payloadMessage.duplicate=!0),s.payloadMessage.retained&&(o.payloadMessage.retained=!0),0===n.indexOf("Sent:")&&(void 0===s.sequence&&(s.sequence=++this._sequence),o.sequence=s.sequence);break;default:throw Error(r(i.INVALID_STORED_DATA,[key,o]))}try{e.setStorageSync(n+this._localKey+s.messageIdentifier,JSON.stringify(o))}catch(e){console.log(e)}},m.prototype.restore=function(n){var s=e.getStorageSync(n),o=JSON.parse(s),a=new u(o.type,o);switch(o.type){case t.PUBLISH:for(var c=o.payloadMessage.payloadHex,h=new ArrayBuffer(c.length/2),d=new Uint8Array(h),l=0;c.length>=2;){var f=parseInt(c.substring(0,2),16);c=c.substring(2,c.length),d[l++]=f}var _=new M(d);_.qos=o.payloadMessage.qos,_.destinationName=o.payloadMessage.destinationName,o.payloadMessage.duplicate&&(_.duplicate=!0),o.payloadMessage.retained&&(_.retained=!0),a.payloadMessage=_;break;default:throw Error(r(i.INVALID_STORED_DATA,[n,s]))}0===n.indexOf("Sent:"+this._localKey)?(a.payloadMessage.duplicate=!0,this._sentMessages[a.messageIdentifier]=a):0===n.indexOf("Received:"+this._localKey)&&(this._receivedMessages[a.messageIdentifier]=a)},m.prototype._process_queue=function(){for(var e=null,t=this._msg_queue.reverse();e=t.pop();)this._socket_send(e),this._notify_msg_sent[e]&&(this._notify_msg_sent[e](),delete this._notify_msg_sent[e])},m.prototype._requires_ack=function(e){var n=Object.keys(this._sentMessages).length;if(n>this.maxMessageIdentifier)throw Error("Too many messages:"+n);for(;void 0!==this._sentMessages[this._message_identifier];)this._message_identifier++;e.messageIdentifier=this._message_identifier,this._sentMessages[e.messageIdentifier]=e,e.type===t.PUBLISH&&this.store("Sent:",e),this._message_identifier===this.maxMessageIdentifier&&(this._message_identifier=1)},m.prototype._on_socket_open=function(e){var n=new u(t.CONNECT,this.connectOptions);n.clientId=this.clientId,this._socket_send(n)},m.prototype._on_socket_message=function(e){this._trace("Client._on_socket_message",e.data);for(var t=this._deframeMessages(e.data),n=0;n<t.length;n+=1)this._handleMessage(t[n])},m.prototype._deframeMessages=function(e){var t=new Uint8Array(e),n=[];if(this.receiveBuffer){var s=new Uint8Array(this.receiveBuffer.length+t.length);s.set(this.receiveBuffer),s.set(t,this.receiveBuffer.length),t=s,delete this.receiveBuffer}try{for(var o=0;o<t.length;){var a=d(t,o),c=a[0];if(o=a[1],null===c)break;n.push(c)}o<t.length&&(this.receiveBuffer=t.subarray(o))}catch(e){var h="undefined"==e.hasOwnProperty("stack")?e.stack.toString():"No Error Stack Available";return void this._disconnected(i.INTERNAL_ERROR.code,r(i.INTERNAL_ERROR,[e.message,h]))}return n},m.prototype._handleMessage=function(n){this._trace("Client._handleMessage",n);try{switch(n.type){case t.CONNACK:if(this._connectTimeout.cancel(),this._reconnectTimeout&&this._reconnectTimeout.cancel(),this.connectOptions.cleanSession){for(var s in this._sentMessages){var a=this._sentMessages[s];e.removeStorageSync("Sent:"+this._localKey+a.messageIdentifier)}for(var s in this._sentMessages={},this._receivedMessages){var c=this._receivedMessages[s];e.removeStorageSync("Received:"+this._localKey+c.messageIdentifier)}this._receivedMessages={}}if(0!==n.returnCode){this._disconnected(i.CONNACK_RETURNCODE.code,r(i.CONNACK_RETURNCODE,[n.returnCode,o[n.returnCode]]));break}this.connected=!0,this.connectOptions.uris&&(this.hostIndex=this.connectOptions.uris.length);var h=[];for(var d in this._sentMessages)this._sentMessages.hasOwnProperty(d)&&h.push(this._sentMessages[d]);if(this._buffered_msg_queue.length>0)for(var l=null,f=this._buffered_msg_queue.reverse();l=f.pop();)h.push(l),this.onMessageDelivered&&(this._notify_msg_sent[l]=this.onMessageDelivered(l.payloadMessage));for(var _=0,g=(h=h.sort(function(e,t){return e.sequence-t.sequence})).length;_<g;_++){if((a=h[_]).type==t.PUBLISH&&a.pubRecReceived){var p=new u(t.PUBREL,{messageIdentifier:a.messageIdentifier});this._schedule_message(p)}else this._schedule_message(a)}this.connectOptions.onSuccess&&this.connectOptions.onSuccess({invocationContext:this.connectOptions.invocationContext});var v=!1;this._reconnecting&&(v=!0,this._reconnectInterval=1,this._reconnecting=!1),this._connected(v,this._wsuri),this._process_queue();break;case t.PUBLISH:this._receivePublish(n);break;case t.PUBACK:(a=this._sentMessages[n.messageIdentifier])&&(delete this._sentMessages[n.messageIdentifier],e.removeStorageSync("Sent:"+this._localKey+n.messageIdentifier),this.onMessageDelivered&&this.onMessageDelivered(a.payloadMessage));break;case t.PUBREC:if(a=this._sentMessages[n.messageIdentifier]){a.pubRecReceived=!0;p=new u(t.PUBREL,{messageIdentifier:n.messageIdentifier});this.store("Sent:",a),this._schedule_message(p)}break;case t.PUBREL:c=this._receivedMessages[n.messageIdentifier];e.removeStorageSync("Received:"+this._localKey+n.messageIdentifier),c&&(this._receiveMessage(c),delete this._receivedMessages[n.messageIdentifier]);var y=new u(t.PUBCOMP,{messageIdentifier:n.messageIdentifier});this._schedule_message(y);break;case t.PUBCOMP:a=this._sentMessages[n.messageIdentifier];delete this._sentMessages[n.messageIdentifier],e.removeStorageSync("Sent:"+this._localKey+n.messageIdentifier),this.onMessageDelivered&&this.onMessageDelivered(a.payloadMessage);break;case t.SUBACK:(a=this._sentMessages[n.messageIdentifier])&&(a.timeOut&&a.timeOut.cancel(),128===n.returnCode[0]?a.onFailure&&a.onFailure(n.returnCode):a.onSuccess&&a.onSuccess(n.returnCode),delete this._sentMessages[n.messageIdentifier]);break;case t.UNSUBACK:(a=this._sentMessages[n.messageIdentifier])&&(a.timeOut&&a.timeOut.cancel(),a.callback&&a.callback(),delete this._sentMessages[n.messageIdentifier]);break;case t.PINGRESP:this.sendPinger.reset();break;case t.DISCONNECT:this._disconnected(i.INVALID_MQTT_MESSAGE_TYPE.code,r(i.INVALID_MQTT_MESSAGE_TYPE,[n.type]));break;default:this._disconnected(i.INVALID_MQTT_MESSAGE_TYPE.code,r(i.INVALID_MQTT_MESSAGE_TYPE,[n.type]))}}catch(e){var I="undefined"==e.hasOwnProperty("stack")?e.stack.toString():"No Error Stack Available";return void this._disconnected(i.INTERNAL_ERROR.code,r(i.INTERNAL_ERROR,[e.message,I]))}},m.prototype._on_socket_error=function(e){this._reconnecting||this._disconnected(i.SOCKET_ERROR.code,r(i.SOCKET_ERROR,[e.data]))},m.prototype._on_socket_close=function(){this._reconnecting||this._disconnected(i.SOCKET_CLOSE.code,r(i.SOCKET_CLOSE))},m.prototype._socket_send=function(t){if(1==t.type){var n=this._traceMask(t,"password");this._trace("Client._socket_send",n)}else this._trace("Client._socket_send",t);e.sendSocketMessage({data:t.encode(),success:function(){},fail:function(){},complete:function(){}}),this.sendPinger.reset()},m.prototype._receivePublish=function(e){switch(e.payloadMessage.qos){case"undefined":case 0:this._receiveMessage(e);break;case 1:var n=new u(t.PUBACK,{messageIdentifier:e.messageIdentifier});this._schedule_message(n),this._receiveMessage(e);break;case 2:this._receivedMessages[e.messageIdentifier]=e,this.store("Received:",e);var s=new u(t.PUBREC,{messageIdentifier:e.messageIdentifier});this._schedule_message(s);break;default:throw Error("Invaild qos="+wireMmessage.payloadMessage.qos)}},m.prototype._receiveMessage=function(e){this.onMessageArrived&&this.onMessageArrived(e.payloadMessage)},m.prototype._connected=function(e,t){this.onConnected&&this.onConnected(e,t)},m.prototype._reconnect=function(){this._trace("Client._reconnect"),this.connected||(this._reconnecting=!0,this.sendPinger.cancel(),this.receivePinger.cancel(),this._reconnectInterval<128&&(this._reconnectInterval=2*this._reconnectInterval),this.connectOptions.uris?(this.hostIndex=0,this._doConnect(this.connectOptions.uris[0])):this._doConnect(this.uri))},m.prototype._disconnected=function(e,t){if(this._trace("Client._disconnected",e,t),void 0!==e&&this._reconnecting)this._reconnectTimeout=new I(this,this._reconnectInterval,this._reconnect);else if(this.sendPinger.cancel(),this.receivePinger.cancel(),this._connectTimeout&&(this._connectTimeout.cancel(),this._connectTimeout=null),this._msg_queue=[],this._buffered_msg_queue=[],this._notify_msg_sent={},this.connectOptions.uris&&this.hostIndex<this.connectOptions.uris.length-1)this.hostIndex++,this._doConnect(this.connectOptions.uris[this.hostIndex]);else if(void 0===e&&(e=i.OK.code,t=r(i.OK)),this.connected){if(this.connected=!1,this.onConnectionLost&&this.onConnectionLost({errorCode:e,errorMessage:t,reconnect:this.connectOptions.reconnect,uri:this._wsuri}),e!==i.OK.code&&this.connectOptions.reconnect)return this._reconnectInterval=1,void this._reconnect()}else 4===this.connectOptions.mqttVersion&&!1===this.connectOptions.mqttVersionExplicit?(this._trace("Failed to connect V4, dropping back to V3"),this.connectOptions.mqttVersion=3,this.connectOptions.uris?(this.hostIndex=0,this._doConnect(this.connectOptions.uris[0])):this._doConnect(this.uri)):this.connectOptions.onFailure&&this.connectOptions.onFailure({invocationContext:this.connectOptions.invocationContext,errorCode:e,errorMessage:t})},m.prototype._trace=function(){if(this.traceFunction){for(var e in arguments)void 0!==arguments[e]&&arguments.splice(e,1,JSON.stringify(arguments[e]));var t=Array.prototype.slice.call(arguments).join("");this.traceFunction({severity:"Debug",message:t})}if(null!==this._traceBuffer){e=0;for(var n=arguments.length;e<n;e++)this._traceBuffer.length==this._MAX_TRACE_ENTRIES&&this._traceBuffer.shift(),0===e?this._traceBuffer.push(arguments[e]):void 0===arguments[e]?this._traceBuffer.push(arguments[e]):this._traceBuffer.push(" "+JSON.stringify(arguments[e]))}},m.prototype._traceMask=function(e,t){var n={};for(var s in e)e.hasOwnProperty(s)&&(n[s]=s==t?"******":e[s]);return n};var E=function(e,t,s,o){var c;if("string"!=typeof e)throw new Error(r(i.INVALID_TYPE,[void 0===e?"undefined":a(e),"host"]));if(2==arguments.length){var h=(c=e).match(/^(wss?):\/\/((\[(.+)\])|([^\/]+?))(:(\d+))?(\/.*)$/);if(!h)throw new Error(r(i.INVALID_ARGUMENT,[e,"host"]));e=h[4]||h[2],t=parseInt(h[7]),s=h[8]}else{if(3==arguments.length&&(o=s,s="/mqtt"),"string"!=typeof s)throw new Error(r(i.INVALID_TYPE,[void 0===s?"undefined":a(s),"path"]));var u=-1!==e.indexOf(":")&&"["!==e.slice(0,1)&&"]"!==e.slice(-1);c=t?"ws://"+(u?"["+e+"]":e)+":"+t+s:"ws://"+(u?"["+e+"]":e)+s}for(var d=0,l=0;l<o.length;l++){var f=o.charCodeAt(l);55296<=f&&f<=56319&&l++,d++}if("string"!=typeof o||d>65535)throw new Error(r(i.INVALID_ARGUMENT,[o,"clientId"]));var _=new m(c,e,t,s,o);this._getHost=function(){return e},this._setHost=function(){throw new Error(r(i.UNSUPPORTED_OPERATION))},this._getPort=function(){return t},this._setPort=function(){throw new Error(r(i.UNSUPPORTED_OPERATION))},this._getPath=function(){return s},this._setPath=function(){throw new Error(r(i.UNSUPPORTED_OPERATION))},this._getURI=function(){return c},this._setURI=function(){throw new Error(r(i.UNSUPPORTED_OPERATION))},this._getClientId=function(){return _.clientId},this._setClientId=function(){throw new Error(r(i.UNSUPPORTED_OPERATION))},this._getOnConnected=function(){return _.onConnected},this._setOnConnected=function(e){if("function"!=typeof e)throw new Error(r(i.INVALID_TYPE,[void 0===e?"undefined":a(e),"onConnected"]));_.onConnected=e},this._getDisconnectedPublishing=function(){return _.disconnectedPublishing},this._setDisconnectedPublishing=function(e){_.disconnectedPublishing=e},this._getDisconnectedBufferSize=function(){return _.disconnectedBufferSize},this._setDisconnectedBufferSize=function(e){_.disconnectedBufferSize=e},this._getOnConnectionLost=function(){return _.onConnectionLost},this._setOnConnectionLost=function(e){if("function"!=typeof e)throw new Error(r(i.INVALID_TYPE,[void 0===e?"undefined":a(e),"onConnectionLost"]));_.onConnectionLost=e},this._getOnMessageDelivered=function(){return _.onMessageDelivered},this._setOnMessageDelivered=function(e){if("function"!=typeof e)throw new Error(r(i.INVALID_TYPE,[void 0===e?"undefined":a(e),"onMessageDelivered"]));_.onMessageDelivered=e},this._getOnMessageArrived=function(){return _.onMessageArrived},this._setOnMessageArrived=function(e){if("function"!=typeof e)throw new Error(r(i.INVALID_TYPE,[void 0===e?"undefined":a(e),"onMessageArrived"]));_.onMessageArrived=e},this._getTrace=function(){return _.traceFunction},this._setTrace=function(e){if("function"!=typeof e)throw new Error(r(i.INVALID_TYPE,[void 0===e?"undefined":a(e),"onTrace"]));_.traceFunction=e},this.connect=function(e){if(n(e=e||{},{timeout:"number",userName:"string",password:"string",willMessage:"object",keepAliveInterval:"number",cleanSession:"boolean",useSSL:"boolean",invocationContext:"object",onSuccess:"function",onFailure:"function",hosts:"object",ports:"object",reconnect:"boolean",mqttVersion:"number",mqttVersionExplicit:"boolean",uris:"object"}),void 0===e.keepAliveInterval&&(e.keepAliveInterval=60),e.mqttVersion>4||e.mqttVersion<3)throw new Error(r(i.INVALID_ARGUMENT,[e.mqttVersion,"connectOptions.mqttVersion"]));if(void 0===e.mqttVersion?(e.mqttVersionExplicit=!1,e.mqttVersion=4):e.mqttVersionExplicit=!0,void 0!==e.password&&void 0===e.userName)throw new Error(r(i.INVALID_ARGUMENT,[e.password,"connectOptions.password"]));if(e.willMessage){if(!(e.willMessage instanceof M))throw new Error(r(i.INVALID_TYPE,[e.willMessage,"connectOptions.willMessage"]));if(e.willMessage.stringPayload=null,void 0===e.willMessage.destinationName)throw new Error(r(i.INVALID_TYPE,[a(e.willMessage.destinationName),"connectOptions.willMessage.destinationName"]))}if(void 0===e.cleanSession&&(e.cleanSession=!0),e.hosts){if(!(e.hosts instanceof Array))throw new Error(r(i.INVALID_ARGUMENT,[e.hosts,"connectOptions.hosts"]));if(e.hosts.length<1)throw new Error(r(i.INVALID_ARGUMENT,[e.hosts,"connectOptions.hosts"]));for(var t=!1,o=0;o<e.hosts.length;o++){if("string"!=typeof e.hosts[o])throw new Error(r(i.INVALID_TYPE,[a(e.hosts[o]),"connectOptions.hosts["+o+"]"]));if(/^(wss?):\/\/((\[(.+)\])|([^\/]+?))(:(\d+))?(\/.*)$/.test(e.hosts[o])){if(0===o)t=!0;else if(!t)throw new Error(r(i.INVALID_ARGUMENT,[e.hosts[o],"connectOptions.hosts["+o+"]"]))}else if(t)throw new Error(r(i.INVALID_ARGUMENT,[e.hosts[o],"connectOptions.hosts["+o+"]"]))}if(t)e.uris=e.hosts;else{if(!e.ports)throw new Error(r(i.INVALID_ARGUMENT,[e.ports,"connectOptions.ports"]));if(!(e.ports instanceof Array))throw new Error(r(i.INVALID_ARGUMENT,[e.ports,"connectOptions.ports"]));if(e.hosts.length!==e.ports.length)throw new Error(r(i.INVALID_ARGUMENT,[e.ports,"connectOptions.ports"]));e.uris=[];for(o=0;o<e.hosts.length;o++){if("number"!=typeof e.ports[o]||e.ports[o]<0)throw new Error(r(i.INVALID_TYPE,[a(e.ports[o]),"connectOptions.ports["+o+"]"]));var h=e.hosts[o],u=e.ports[o],d=-1!==h.indexOf(":");c="ws://"+(d?"["+h+"]":h)+":"+u+s,e.uris.push(c)}}}_.connect(e)},this.subscribe=function(e,t){if("string"!=typeof e)throw new Error("Invalid argument:"+e);if(n(t=t||{},{qos:"number",invocationContext:"object",onSuccess:"function",onFailure:"function",timeout:"number"}),t.timeout&&!t.onFailure)throw new Error("subscribeOptions.timeout specified with no onFailure callback.");if(void 0!==t.qos&&0!==t.qos&&1!==t.qos&&2!==t.qos)throw new Error(r(i.INVALID_ARGUMENT,[t.qos,"subscribeOptions.qos"]));_.subscribe(e,t)},this.unsubscribe=function(e,t){if("string"!=typeof e)throw new Error("Invalid argument:"+e);if(n(t=t||{},{invocationContext:"object",onSuccess:"function",onFailure:"function",timeout:"number"}),t.timeout&&!t.onFailure)throw new Error("unsubscribeOptions.timeout specified with no onFailure callback.");_.unsubscribe(e,t)},this.send=function(e,t,n,s){var o;if(0===arguments.length)throw new Error("Invalid argument.length");if(1==arguments.length){if(!(e instanceof M)&&"string"!=typeof e)throw new Error("Invalid argument:"+(void 0===e?"undefined":a(e)));if(void 0===(o=e).destinationName)throw new Error(r(i.INVALID_ARGUMENT,[o.destinationName,"Message.destinationName"]));_.send(o)}else(o=new M(t)).destinationName=e,arguments.length>=3&&(o.qos=n),arguments.length>=4&&(o.retained=s),_.send(o)},this.publish=function(e,t,n,s){var o;if(console.log("Publising message to: ",e),0===arguments.length)throw new Error("Invalid argument.length");if(1==arguments.length){if(!(e instanceof M)&&"string"!=typeof e)throw new Error("Invalid argument:"+(void 0===e?"undefined":a(e)));if(void 0===(o=e).destinationName)throw new Error(r(i.INVALID_ARGUMENT,[o.destinationName,"Message.destinationName"]));_.send(o)}else(o=new M(t)).destinationName=e,arguments.length>=3&&(o.qos=n),arguments.length>=4&&(o.retained=s),_.send(o)},this.disconnect=function(){_.disconnect()},this.getTraceLog=function(){return _.getTraceLog()},this.startTrace=function(){_.startTrace()},this.stopTrace=function(){_.stopTrace()},this.isConnected=function(){return _.connected}};E.prototype={get host(){return this._getHost()},set host(e){this._setHost(e)},get port(){return this._getPort()},set port(e){this._setPort(e)},get path(){return this._getPath()},set path(e){this._setPath(e)},get clientId(){return this._getClientId()},set clientId(e){this._setClientId(e)},get onConnected(){return this._getOnConnected()},set onConnected(e){this._setOnConnected(e)},get disconnectedPublishing(){return this._getDisconnectedPublishing()},set disconnectedPublishing(e){this._setDisconnectedPublishing(e)},get disconnectedBufferSize(){return this._getDisconnectedBufferSize()},set disconnectedBufferSize(e){this._setDisconnectedBufferSize(e)},get onConnectionLost(){return this._getOnConnectionLost()},set onConnectionLost(e){this._setOnConnectionLost(e)},get onMessageDelivered(){return this._getOnMessageDelivered()},set onMessageDelivered(e){this._setOnMessageDelivered(e)},get onMessageArrived(){return this._getOnMessageArrived()},set onMessageArrived(e){this._setOnMessageArrived(e)},get trace(){return this._getTrace()},set trace(e){this._setTrace(e)}};var M=function(e){var t,n;if(!("string"==typeof e||e instanceof ArrayBuffer||e instanceof Int8Array||e instanceof Uint8Array||e instanceof Int16Array||e instanceof Uint16Array||e instanceof Int32Array||e instanceof Uint32Array||e instanceof Float32Array||e instanceof Float64Array))throw r(i.INVALID_ARGUMENT,[e,"newPayload"]);t=e,this._getPayloadString=function(){return"string"==typeof t?t:v(t,0,t.length)},this._getPayloadBytes=function(){if("string"==typeof t){var e=new ArrayBuffer(g(t)),n=new Uint8Array(e);return p(t,n,0),n}return t},this._getDestinationName=function(){return n},this._setDestinationName=function(e){if("string"!=typeof e)throw new Error(r(i.INVALID_ARGUMENT,[e,"newDestinationName"]));n=e};var s=0;this._getQos=function(){return s},this._setQos=function(e){if(0!==e&&1!==e&&2!==e)throw new Error("Invalid argument:"+e);s=e};var o=!1;this._getRetained=function(){return o},this._setRetained=function(e){if("boolean"!=typeof e)throw new Error(r(i.INVALID_ARGUMENT,[e,"newRetained"]));o=e};var a=!1;this._getDuplicate=function(){return a},this._setDuplicate=function(e){a=e}};return M.prototype={get payloadString(){return this._getPayloadString()},get payloadBytes(){return this._getPayloadBytes()},get destinationName(){return this._getDestinationName()},set destinationName(e){this._setDestinationName(e)},get topic(){return this._getDestinationName()},set topic(e){this._setDestinationName(e)},get qos(){return this._getQos()},set qos(e){this._setQos(e)},get retained(){return this._getRetained()},set retained(e){this._setRetained(e)},get duplicate(){return this._getDuplicate()},set duplicate(e){this._setDuplicate(e)}},{Client:E,Message:M}}(wx)},"object"===a(t)&&"object"===a(o)?o.exports=r():void 0===(i="function"==typeof(s=r)?s.call(t,n,t,o):s)||(o.exports=i)}).call(t,function(){return this}(),n(3)(e))},function(e,t){e.exports=function(e){return e.webpackPolyfill||(e.deprecate=function(){},e.paths=[],e.children=[],e.webpackPolyfill=1),e}},function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(){for(var e=[],t=0;t<36;t++)e[t]="0123456789abcdef".substr(Math.floor(16*Math.random()),1);return e[14]="4",e[19]="0123456789abcdef".substr(3&e[19]|8,1),e[8]=e[13]=e[18]=e[23]="-",e.join("")},e.exports=t.default}]);