(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
物联网-微信小程序-mqtt.min.js-真机可直连微信小程序
需积分: 0 168 浏览量
更新于2023-05-10
6
收藏 50KB RAR 举报
已经做好封装,可以在小程序中直接引入模块,然后调用模块里面的函数。可能有小伙伴遇到过微信开发者工具可以连接MQTT服务器,但是一到真实的手机环境中就没办法连接服务器。这个资源可以帮助你解决这个问题,直接替换即可
L-F-D
- 粉丝: 3050
- 资源: 1
最新资源
- STM32F401,使用ST-link时候,不能识别,显示ST-LINK USB communication error
- 快速排序算法Python实现:详解分治法原理与高效排序步骤
- 陀螺仪选型陀螺仪陀螺仪选型型陀螺仪选型
- Intouch2020R2SP1与西门子1500PLC通讯配置手册
- 英特尔2021-2024年网络连接性和IPU路线图
- RuoYi-Cloud-Plus 微服务通用权限管理系统
- 家庭用具检测15-YOLO(v8至v11)数据集合集.rar
- deploy.yaml
- PHP快速排序算法实现与优化
- 2023-04-06-项目笔记 - 第三百五十五阶段 - 4.4.2.353全局变量的作用域-353 -2025.12.22