收藏
回答

微信小程序 mqtt我想在APP.JS连接服务器,在子页面选择topic,如何拆分?

APP.js无法调用子页面的方法,

库paho-mqtt-min.js

 doConnect: function () {

        var that = this;

        if (that.data.client && that.data.client.isConnected()) {

            wx.showToast({

                title: '不要重复连接'

            })

            return

        }

        var client = new Client('wss://ixxxxxxm/mqtt', that.randomString());

 

        client.connect({

            useSSL: true,

            cleanSession: true,

            keepAliveInterval: 5,

            onSuccess: function () {

                that.data.client = client

                that.onConnectSuccess(client)//在子页面

                //收到信息并处理//在子页面

                client.onMessageArrived = function (msg) {

                    that.onMessageArrived(msg)}

                //异常//在子页面

                client.onConnectionLost = function (responseObject) {

                   that.onConnectionLost(responseObject)

                }

            },

            onFailure(res){

                wx.showModal({

                  title:'连接失败',

                  content:'code:'+res.errorCode+',message:' +res.errorMessage,

                  confirmText:'重连',

                  success(res){

                      if(res.confirm)

                      that.doConnect()

                  }

                })

            }

        });

    },


回答关注问题邀请回答
收藏
登录 后发表内容