收藏
回答

实时数据推送在 iPhone 真机报错,Android不报错?

报错内容:

<Error: errCode: -402009 realtime listener websocket
connection closed | errMsg: No Realtime Listeners,
code 3001, reason The client is terminating the
connection because no more realtime listeners exist
history states: UNINIT-(loginStart)-INIT_LOGGING_IN-(close)->CLOSED>


代码:

onLoad: function (options) {
    this.tick()
    // return
    // 查找对方资料
    let that = this
    let _id = options._id
    let oppositeOpenid = options.oppositeOpenid
    let oppositeDocid = options.oppositeDocid
    console.log('oppositeOpenid : ' + oppositeOpenid + ', oppositeDocid : ' + oppositeDocid + ', docId : ' + _id)
    that.setData({
      docId: _id,
      oppositeOpenid: oppositeOpenid,
      oppositeDocid: oppositeDocid
    })


    // 监听自身 matchpool 状态
    const db = wx.cloud.database()
    let watcher = db.collection('matchpool').doc(_id).watch({
      onChange: function (snapshot) {
        console.log('snapshot', snapshot)
        console.log('docs\'s changed events', snapshot.docChanges)
        console.log('query result snapshot after the event', snapshot.docs)
        console.log('is init data', snapshot.type === 'init')
        if (snapshot.docChanges.length > 0) {
          let docChange = snapshot.docChanges[0]
          let dataType = docChange.dataType
          console.log('dataType : ' + dataType)
          if (dataType == 'remove') {
            console.log('对方已退出本次匹配')
            watcher.close()
            that.setData({
              oppositeExit: true
            })
            wx.showModal({
              showCancel: true,
              title: '退出本次会话?',
              content: '对方已退出,立即退出吗?',
              success: res => {
                if (res.confirm) {
                  console.log('用户点击确定')
                  wx.navigateBack({
                    delta: 1,
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        }
      },
      onError: function (err) {
        console.log(err)
      }
    })
    that.setData({
      watcher: watcher
    })


    db.collection('cuser').where({
      _openid: oppositeOpenid
    }).get().then(res => {
      console.log('opposite cuser : ', res)
      if (res.data.length >= 1) {
        let oppositeCuser = res.data[0]
        that.setData({
          oppositeCuser: oppositeCuser
        })
      }
    }).catch(err => {
      console.log('err : ', err)
    })
  },
回答关注问题邀请回答
收藏

2 个回答

登录 后发表内容