123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- var mqtt = require("../utils/mqtt.min.js");
- var client = null;
- var timeout = null;
- var interval = 100;
- var publish_flag = false;
- var publish_timeout=0;
- var subscribe_flag = false;
- var subscribe_timeout=0;
- var subscribe_multiple_flag = false
- var subscribe_multiple_timeout=0;
- var client_state = false;
- var onMessageArrivedCallBack;
- var onConnectionSuccessCallBack;
- var onConnectionLostCallBack;
- var host = 'wxs://mnifdv.cn/mqtt';
- const options = {
- keepalive: 30,
- ssl: true,
- clean: true,
- protocolVersion: 4,
- username: 'yang',
- password: '11223344',
- clientId: (+new Date()) + '' + Math.ceil(Math.random() * 1000000000),
- reconnectPeriod: 4 * 1000,
- connectTimeout: 3 * 1000,
-
-
- }
- var SetonConnectionSuccessCallBack = function SetonConnectionSuccessCallBack(fun) {
- onConnectionSuccessCallBack = fun;
- }
- var SetonConnectionLostCallBack = function SetonConnectionLostCallBack(fun) {
- onConnectionLostCallBack = fun;
- }
- var publishTopic = function publishTopic(topic, payload, qos, retained, SuccessFun) {
- if (client_state){
- let opts = {};
- opts.qos = qos;
- opts.retain = retained;
- publish_flag = true;
-
-
-
-
-
-
-
-
- client.publish(topic, payload, opts, function (err,err1) {
- console.log("发布消息:" + topic +" "+ payload);
- publish_flag = false;
- console.log("清除publish_timeout" + err + " " + err1);
- if (SuccessFun !=null){
- SuccessFun(topic, payload, qos, retained);
- }
- });
- }
- }
- var SetonMessageArrivedCallBack = function SetonMessageArrivedCallBack(fun) {
- onMessageArrivedCallBack = fun;
- }
- var subscribeTopic = function subscribeTopic(topic, q, SuccessFun, FailureFun) {
- if (client_state) {
- subscribe_flag = true;
-
-
-
-
-
-
-
-
-
- client.unsubscribe(topic, function (err, granted) {
-
- });
- client.subscribe(topic, { qos: q }, function (err, granted) {
- try {
- console.log("订阅:" + err, granted, granted.length);
- subscribe_flag = false;
-
- if (granted.length != 0) {
- if (SuccessFun != null) SuccessFun(granted[0]);
- } else {
- if (FailureFun != null) FailureFun({ topic: topic, qos: q });
- }
- } catch (e) {
-
- }
- });
- }
- }
- var subscribeTopicMultiple = function subscribeTopicMultiple(filter, SuccessFun, FailureFun) {
- if (client_state) {
-
-
-
-
-
-
-
-
- let i = 0;
- let topic = [];
- Object.keys(filter).forEach(function (key) {
-
- topic[i] = filter[key];
- i++;
- });
-
- client.unsubscribe(topic, function (err) {
-
- });
- subscribe_multiple_flag = true;
- client.subscribe(filter, function (err, granted) {
- try {
- console.log("多重订阅:" + err, granted, granted.length);
- subscribe_multiple_flag = false;
-
- if (granted.length != 0) {
- if (SuccessFun != null) SuccessFun(filter);
- } else {
- if (FailureFun != null) FailureFun(filter);
- }
- } catch (e) { }
- });
- }
- }
- var unSubscribeTopic = function unSubscribeTopic(topic, SuccessFun) {
-
- if (client_state) {
- client.unsubscribe(topic, function (err) {
- if (SuccessFun != null){
- SuccessFun();
- }
- console.log("取消订阅:" + topic);
- });
- }
- }
- function timeout_function(param) {
- if (publish_flag){
- publish_timeout = publish_timeout+1;
- if (publish_timeout >= 30){
- publish_timeout = 0;
-
- publish_flag = false;
- subscribe_flag = false;
- subscribe_multiple_flag = false;
- if (client_state) {
- try { client.reconnect(); } catch (e) { }
- client_state = false;
- }
- }
- }
- else {
- publish_timeout = 0;
- }
- if (subscribe_flag) {
- subscribe_timeout = subscribe_timeout+1;
- if (subscribe_timeout>30){
- subscribe_timeout=0;
- publish_flag = false;
- subscribe_flag = false;
- subscribe_multiple_flag = false;
- if (client_state) {
- try { client.reconnect(); } catch (e) { }
- client_state = false;
- }
- }
- }
- else{
- subscribe_timeout=0;
- }
- if (subscribe_multiple_flag){
- subscribe_multiple_timeout = subscribe_multiple_timeout+1;
- if (subscribe_multiple_timeout>30){
- subscribe_multiple_timeout = 0;
- publish_flag = false;
- subscribe_flag = false;
- subscribe_multiple_flag = false;
- if (client_state) {
- try { client.reconnect(); } catch (e) { }
- client_state = false;
- }
- }
- }
- else{
- subscribe_multiple_timeout=0;
- }
- }
- var ConnectMqtt = function ConnectMqtt() {
- console.log(options);
- try { client.end(); } catch (e) { }
- client = mqtt.connect(host, options)
- client.on('connect', function () {
- client_state = true;
- console.log("connect");
- if (timeout != null)
- {
- clearInterval(timeout);
- }
-
- timeout = setInterval(timeout_function, interval, null);
- if (onConnectionSuccessCallBack != null) {
- onConnectionSuccessCallBack();
- }
- });
- client.on('message', function (topic, message) {
- let args = {};
- args.destinationName = topic;
- args.payloadString = message;
- console.log(args);
- if (onMessageArrivedCallBack != null)
- {
- onMessageArrivedCallBack(args);
- }
- })
- client.on('close', function () {
- console.log("close");
- if (onConnectionLostCallBack != null)
- {
- onConnectionLostCallBack("close");
- }
- });
- client.on('disconnect', function () {
- console.log("disconnect");
- if (onConnectionLostCallBack != null)
- {
- onConnectionLostCallBack("disconnect");
- }
- });
- client.on('reconnect', function () {
- console.log("reconnect");
- if (onConnectionLostCallBack != null)
- {
- onConnectionLostCallBack("reconnect");
- }
- });
- client.on('offline', function () {
- console.log("offline");
- if (onConnectionLostCallBack != null)
- {
- onConnectionLostCallBack("offline");
- }
- });
- client.on('error', function () {
- console.log("error");
- if (onConnectionLostCallBack != null)
- {
- onConnectionLostCallBack("error");
- }
- });
- }
- wx.onNetworkStatusChange(function (res) {
- ConnectMqtt();
- if (res.networkType == "none") console.log("无网络");
- else console.log("网络类型:" + res.networkType);
- })
- module.exports = {
- ConnectMqtt: ConnectMqtt,
- SetonConnectionSuccessCallBack: SetonConnectionSuccessCallBack,
- SetonConnectionLostCallBack: SetonConnectionLostCallBack,
- SetonMessageArrivedCallBack: SetonMessageArrivedCallBack,
- publishTopic: publishTopic,
- subscribeTopic: subscribeTopic,
- unSubscribeTopic: unSubscribeTopic,
- subscribeTopicMultiple: subscribeTopicMultiple,
- }
|