j***@163.com
j***@163.com
  • 发布:2020-10-21 11:01
  • 更新:2023-04-10 14:23
  • 阅读:1969

如何在nvue组件中使用mqtt

分类:nvue

vue中使用mqtt没问题,在nvue中就会报错。TypeError: socketTask.onOpen is not a function

connect: function () {  

    console.log('进入页面')  
      var hosts = "",  
        // #ifdef MP-WEIXIN  
        hosts = "wxs://" + this.serve.host + this.serve.path;  
      //#endif  
      // #ifdef APP-PLUS  
      hosts =  
        "wx://" + this.serve.host + ":" + this.serve.port + this.serve.path;  
      //#endif  

      if (this.client == null || this.client.connented == false) {  
        uni.showLoading({  
          title: "连接中···",  
        });  
        this.connectSuccess(false);  
        console.log(hosts, this.options);  
        this.client = mqtt.connect(hosts, this.options);  

        this.client.on("connect", () => {  
          uni.hideLoading();  
          console.log('连接成功')  
          this.showToast("连接成功", 1000, "success");  
          this.connectSuccess(true);  
          this.subscribe();  
        });  
      }  

      this.client.on("reconnect", (error) => {  
        uni.hideLoading();  
        this.showToast("正在重连···", 1000);  
      });  
      this.client.on("error", (error) => {  
        uni.hideLoading();  
        this.showToast("连接失败!", 1000);  
        this.connect();  
      });  
    },
2020-10-21 11:01 负责人:DCloud_UNI_GSQ 分享
已邀请:
DCloud_UNI_GSQ

DCloud_UNI_GSQ

编辑一下 mqtt.min.js:

socketTask = wx.connectSocket({  
                        url: url,  
                        protocols: websocketSubProtocol,  
                        success(){}//增加这行  
                    });
  • 唬唬唬

    请问H5和微信小程序上可以使用uni.createMapContex,但是H5获取不到原生地图对象 plus.maps.Map,微信小程序报错 $getAppMap is not a function 是什么原因呢?已经配置自定义组件模式了

    2020-10-22 09:13

  • DCloud_UNI_GSQ

    回复 唬唬唬: h5没有原生AppMap

    2020-10-22 11:02

  • DCloud_UNI_GSQ

    具体的看一下文档

    2020-10-22 11:03

  • 唬唬唬

    回复 DCloud_UNI_GSQ: 不好意思,因为问了你们好几个人没有回复,官群也是 文档上也不是很详细呀,那微信小程序呢?在微信小程序上报错 $getAppMap is not a function 是什么原因呢?

    2020-10-22 14:18

  • DCloud_UNI_GSQ

    回复 唬唬唬: 文档里搜$getAppMap可以得到如下信息:$getAppMap是用于获取plus。map对象,而plus.map是html5plus(App)环境才有的

    2020-10-22 14:41

  • j***@163.com (作者)

    Thanks♪(・ω・)ノ

    2020-10-22 15:26

  • 唬唬唬

    回复 DCloud_UNI_GSQ: 好的 明白了,十分感谢

    2020-10-22 15:28

  • 香蕉不是笨啦啦

    最新版3.7.9问题还是存在,编译app还是要加这行

    2023-04-10 14:01

DCloud_Android_DQQ

DCloud_Android_DQQ

你这里的mqtt 是一个第三方库还是你自己封装的?
提供一下可以复现的demo 我确认一下

  • j***@163.com (作者)

    https://github.com/asagcs/ceshi

    2020-10-21 11:27

  • j***@163.com (作者)

    这个应该是官方的,另外我npm 4.1.0版官方mqtt也是报同样的错误

    2020-10-21 11:29

  • j***@163.com (作者)

    这个是刚刚写的demo https://github.com/asagcs/ceshi 请看看

    2020-10-21 11:30

  • DCloud_Android_DQQ

    回复 j***@163.com: 好 我试试

    2020-10-21 11:40

要回复问题请先登录注册