(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.mqtt=f()}})(function(){var define,module,exports;return function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r}()({1:[function(require,module,exports){(function(process,global){"use strict";var events=require("events");var Store=require("./store");var eos=require("end-of-stream");var mqttPacket=require("mqtt-packet");var Writable=require("readable-stream").Writable;var inherits=require("inherits");var reInterval=require("reinterval");var validations=require("./validations");var xtend=require("xtend");var setImmediate=global.setImmediate||function(callback){process.nextTick(callback)};var defaultConnectOptions={keepalive:60,reschedulePings:true,protocolId:"MQTT",protocolVersion:4,reconnectPeriod:1e3,connectTimeout:30*1e3,clean:true,resubscribe:true};function defaultId(){return"mqttjs_"+Math.random().toString(16).substr(2,8)}function sendPacket(client,packet,cb){client.emit("packetsend",packet);var result=mqttPacket.writeToStream(packet,client.stream);if(!result&&cb){client.stream.once("drain",cb)}else if(cb){cb()}}function flush(queue){if(queue){Object.keys(queue).forEach(function(messageId){if(typeof queue[messageId]==="function"){queue[messageId](new Error("Connection closed"));delete queue[messageId]}})}}function storeAndSend(client,packet,cb){client.outgoingStore.put(packet,function storedPacket(err){if(err){return cb&&cb(err)}sendPacket(client,packet,cb)})}function nop(){}function MqttClient(streamBuilder,options){var k;var that=this;if(!(this instanceof MqttClient)){return new MqttClient(streamBuilder,options)}this.options=options||{};for(k in defaultConnectOptions){if(typeof this.options[k]==="undefined"){this.options[k]=defaultConnectOptions[k]}else{this.options[k]=options[k]}}this.options.clientId=typeof this.options.clientId==="string"?this.options.clientId:defaultId();this.streamBuilder=streamBuilder;this.outgoingStore=this.options.outgoingStore||new Store;this.incomingStore=this.options.incomingStore||new Store;this.queueQoSZero=this.options.queueQoSZero===undefined?true:this.options.queueQoSZero;this._resubscribeTopics={};this.messageIdToTopic={};this.pingTimer=null;this.connected=false;this.disconnecting=false;this.queue=[];this.connackTimer=null;this.reconnectTimer=null;this.nextId=Math.max(1,Math.floor(Math.random()*65535));this.outgoing={};this.on("connect",function(){if(this.disconnected){return}this.connected=true;var outStore=this.outgoingStore.createStream();this.once("close",remove);outStore.on("end",function(){that.removeListener("close",remove)});outStore.on("error",function(err){that.removeListener("close",remove);that.emit("error",err)});function remove(){outStore.destroy();outStore=null}function storeDeliver(){if(!outStore){return}var packet=outStore.read(1);var cb;if(!packet){outStore.once("readable",storeDeliver);return}if(!that.disconnecting&&!that.reconnectTimer){cb=that.outgoing[packet.messageId];that.outgoing[packet.messageId]=function(err,status){if(cb){cb(err,status)}storeDeliver()};that._sendPacket(packet)}else if(outStore.destroy){outStore.destroy()}}storeDeliver()});this.on("close",function(){this.connected=false;clearTimeout(this.connackTimer)});this.on("connect",this._setupPingTimer);this.on("connect",function(){var queue=this.queue;function deliver(){var entry=queue.shift();var packet=null;if(!entry){return}packet=entry.packet;that._sendPacket(packet,function(err){if(entry.cb){entry.cb(err)}deliver()})}deliver()});var firstConnection=true;this.on("connect",function(){if(!firstConnection&&this.options.clean&&Object.keys(this._resubscribeTopics).length>0){if(this.options.resubscribe){this._resubscribeTopics.resubscribe=true;this.subscribe(this._resubscribeTopics)}else{this._resubscribeTopics={}}}firstConnection=false});this.on("close",function(){if(that.pingTimer!==null){that.pingTimer.clear();that.pingTimer=null}});this.on("close",this._setupReconnect);events.EventEmitter.call(this);this._setupStream()}inherits(MqttClient,events.EventEmitter);MqttClient.prototype._setupStream=function(){var connectPacket;var that=this;var writable=new Writable;var parser=mqttPacket.parser(this.options);var completeParse=null;var packets=[];this._clearReconnect();this.stream=this.streamBuilder(this);parser.on("packet",function(packet){packets.push(packet)});function nextTickWork(){process.nextTick(work)}function work(){var packet=packets.shift();var done=completeParse;if(packet){that._handlePacket(packet,nextTickWork)}else{completeParse=null;done()}}writable._write=function(buf,enc,done){completeParse=done;parser.parse(buf);work()};this.stream.pipe(writable);this.stream.on("error",nop);eos(this.stream,this.emit.bind(this,"close"));connectPacket=Object.create(this.options);connectPacket.cmd="connect";sendPacket(this,connectPacket);parser.on("error",this.emit.bind(this,"error"));this.stream.setMaxListeners(1e3);clearTimeout(this.connackTimer);this.connackTimer=setTimeout(function(){that._cleanUp(true)},this.options.connectTimeout)};MqttClient.prototype._handlePacket=function(packet,done){this.emit("packetreceive",packet);switch(packet.cmd){case"publish":this._handlePublish(packet,done);break;case"puback":case"pubrec":case"pubcomp":case"suback":case"unsuback":this._handleAck(packet);done();break;case"pubrel":this._handlePubrel(packet,done);break;case"connack":this._handleConnack(packet);done();break;case"pingresp":this._handlePingresp(packet);done();break;default:break}};MqttClient.prototype._checkDisconnecting=function(callback){if(this.disconnecting){if(callback){callback(new Error("client disconnecting"))}else{this.emit("error",new Error("client disconnecting"))}}return this.disconnecting};MqttClient.prototype.publish=function(topic,message,opts,callback){var packet;if(typeof opts==="function"){callback=opts;opts=null}var defaultOpts={qos:0,retain:false,dup:false};opts=xtend(defaultOpts,opts);if(this._checkDisconnecting(callback)){return this}packet={cmd:"publish",topic:topic,payload:message,qos:opts.qos,retain:opts.retain,messageId:this._nextId(),dup:opts.dup};switch(opts.qos){case 1:case 2:this.outgoing[packet.messageId]=callback||nop;this._sendPacket(packet);break;default:this._sendPacket(packet,callback);break}return this};MqttClient.prototype.subscribe=function(){var packet;var args=Array.prototype.slice.call(arguments);var subs=[];var obj=args.shift();var resubscribe=obj.resubscribe;var callback=args.pop()||nop;var opts=args.pop();var invalidTopic;var that=this;delete obj.resubscribe;if(typeof obj==="string"){obj=[obj]}if(typeof callback!=="function"){opts=callback;callback=nop}invalidTopic=validations.validateTopics(obj);if(invalidTopic!==null){setImmediate(callback,new Error("Invalid topic "+invalidTopic));return this}if(this._checkDisconnecting(callback)){return this}var defaultOpts={qos:0};opts=xtend(defaultOpts,opts);if(Array.isArray(obj)){obj.forEach(function(topic){if(that._resubscribeTopics[topic]<opts.qos||!that._resubscribeTopics.hasOwnProperty(topic)||resubscribe){subs.push({topic:topic,qos:opts.qos})}})}else{Object.keys(obj).forEach(function(k){if(that._resubscribeTopics[k]<obj[k]||!that._resubscribeTopics.hasOwnProperty(k)||resubscribe){subs.push({topic:k,qos:obj[k]})}})}packet={cmd:"subscribe",subscriptions:subs,qos:1,retain:false,dup:false,messageId:this._nextId()};if(!subs.length){callback(null,[]);return}if(this.o
L-F-D
- 粉丝: 2917
- 资源: 1
最新资源
- C#ASP.NET企业在线记账平台源码数据库 SQL2012源码类型 WebForm
- PHP客户关系CRM管理系统源码数据库 MySQL源码类型 WebForm
- python-勇者斗恶龙 回合制游戏 有图有真相 英雄和怪兽行为和状态的设定
- JAVA的Springboot垃圾分类识别小程序源码带部署文档数据库 MySQL源码类型 WebForm
- 图像分类数据集:番茄叶片病害图像识别数据集(包括划分好的数据【文件夹保存】、类别字典文件)
- web版本实现迅飞语音听写(流式版)封装代码
- JAVAspringboot校园转转二手电商市场源码数据库 MySQL源码类型 WebForm
- MICO_Android_Release_2_4_54.apk
- 个人日常总结,待整理 杂乱的笔记
- liteidex38.3-win64
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈