文档

小程序paho-mqtt链接mqtt

微信扫一扫,分享到朋友圈

小程序paho-mqtt链接mqtt
收藏 0 0
1:引入    import { Client, Message} from ‘./utils/paho-mqtt.js’
链接:https://pan.baidu.com/s/1g06oXca11jLjjt12EUr3Ig
提取码:2h5b
复制这段内容后打开百度网盘手机App,操作更方便哦

2:链接方法

//地址   端口    主题
client = new Client(location.hostname, Number(location.port), "clientId");
//断开连接回调
client.onConnectionLost = onConnectionLost;
//收到消息回调
client.onMessageArrived = onMessageArrived;
//连接
client.connect({onSuccess:onConnect});
function onConnect() {
  // 建立连接后,进行订阅并发送消息
  console.log("onConnect");
  client.subscribe("/World");
  //订阅主题
  message = MQTT.Message("Hello");
  message.destinationName = "/World";
  client.send(message);
};
function onConnectionLost(responseObject) {
  //断开了
  if (responseObject.errorCode !== 0)
  console.log("onConnectionLost:"+responseObject.errorMessage);
};
function onMessageArrived(message) {
  //收到消息
  console.log("onMessageArrived:"+message.payloadString);
  client.disconnect();
};

下面是做小程序项目用的这里备份一下(里面一些赋值需要改哦)

doConnect: function (nextPage) {
    let that = this;
    let imCfg = that.globalData.imCfg;
    let organizationCode = that.globalData.organizationCode;
    let groupUser = that.globalData.groupUser;
    if (!imCfg) {
      return
    }
    var client = new Client(imCfg.url, "" + organizationCode + "" + groupUser.id);
    client.connect({
      mqttVersion: 4,
      useSSL: true,
      reconnect: true, // 是否重新连接
      userName: "lhym",
      password: "lhym@",
      cleanSession: true,
      keepAliveInterval: 10,
      onSuccess: function (e) {
        console.log('mqtt', "连接成功ID:" + organizationCode + "" + groupUser.id)
        that.globalData.isIm = true;
        that.globalData.client = client;
        var page1
        try {
          var pages1 = getCurrentPages() || []
          if (pages1.length > 0) {
            pages1.forEach(function (value, key, arr) {
              page1 = pages1[key]
              page1.setData({
                isIm: true
              })
            })
          }
        } catch (e) {
          console.log(e)
        }
        client.onMessageArrived = function (msg) {
          console.log("接收到新新消息",JSON.stringify(msg))
          //接收到消息
          if (msg.destinationName) {
            let messageitem = JSON.parse(msg.payloadString);
            // console.log(messageitem)
            var page;
            try {
              var pages = getCurrentPages() || []
              // console.log(pages)
              if (pages.length > 0) {
                pages.forEach(function (value, key, arr) {
                  page = pages[key]
                  if (page.route) {
                    page.setData({
                      messageitem: messageitem
                    })
                  }
                })
              }
            } catch (e) {
              if (page && page.onMessage) {
                page.onMessage(msg.payloadString)
              }
            }
          }
        }
        client.onConnectionLost = function (responseObject) {
          console.log(responseObject)
          console.log('断开连接')
          that.globalData.isIm = false
          console.log(that.globalData.isIm)
          if (responseObject.errorCode !== 0) {
            console.log("onConnectionLost:" + responseObject.errorMessage);
          }
        }
      },
      onFailure: function (res) {
        console.log('mqtt', "连接失败")
         console.log(res)
      }
    });
  },

 

展开阅读全文
干一件事情,用心干就完了
下一篇

程序员常用的几个DOC命令

你也可能喜欢

发表评论

您的电子邮件地址不会被公开。 必填项已用 * 标注

提示:点击验证后方可评论!

插入图片

分类目录

微信扫一扫

微信扫一扫