收藏
回答

websocket,链接connectSocket:ok,但是没有任何回调?

websocket,链接connectSocket:ok,但是没有任何回调,回调函数写在了onReady里面

下面放上主要的代码

  onReady: function () {
    var that = this;
    var user_id = this.data.user_id
    var friend_id = this.data.friend_id
    var task_group_id = this.data.task_group_id
    var info = this.data.info


    SocketTask.onOpen(res => {
      wx.hideLoading()
      socketOpen = true;
      console.log('监听 WebSocket 连接打开事件。', res)
      // 发送空白消息,登录websocket用
      if (socketOpen) {
        SocketTask.send({
          data: JSON.stringify({
            msg: "",
            user_id: user_id,
            friend_id: friend_id,
            task_group_id: task_group_id
          })
        })
      } else {
        that.webSocket()
      }
    })
    SocketTask.onClose(onClose => {
      console.log('监听 WebSocket 连接关闭事件。', onClose)
      socketOpen = false;
      that.webSocket()
    })
    SocketTask.onError(onError => {
      console.log('监听 WebSocket 错误。错误信息', onError)
      socketOpen = false
      that.webSocket()
    })
    SocketTask.onMessage(onMessage => {
      console.log('监听WebSocket接受到服务器的消息事件。服务器返回的消息', onMessage)
      var ret = JSON.parse(onMessage.data)
      console.log(ret)


      // 如果出错,提示,重连
      if (ret.code == 1) {
        // wx.showToast({
        //   title: ret.msg,
        //   icon: "none"
        // })
        that.webSocket()
        return false;
      } else {


        // 如果是撤回消息
        if (ret.data.data.msg_type == "withdraw") {
          that.delIndexMsg(ret.data.data.index)
          return false;
        }



        if (ret.data.data.selfType) {
          // 接收自己发出消息的自增id
          that.setData({


            chatList: that.data.chatList.concat({
              send_id: user_id,
              user_id: user_id,
              friend_id: user_id,
              msg: ret.data.msg,
              user_avatarUrl: that.data.avatarUrl,
              user_avatarUrl: ret.data.userInfo.avatarUrl,
              create_time: ret.data.data.create_time,
              id: ret.data.data.insert_id,
              type: ret.data.data.type,
              time: ret.data.data.time,
              file_name: ret.data.data.file_name,
              file_size: ret.data.data.file_size
            }),
            toView: "msg" + ret.data.data.insert_id,
            lastId: ret.data.data.insert_id
          })


        } else {
          // 接收其他人的消息
          that.setData({
            chatList: that.data.chatList.concat({
              send_id: ret.data.data.user_id,
              user_id: friend_id,
              friend_id: user_id,
              msg: ret.data.msg,
              friend_avatarUrl: that.data.avatarUrl,
              send_avatarUrl: ret.data.userInfo.avatarUrl,
              create_time: ret.data.create_time,
              id: that.data.lastId,
              type: ret.data.data.type,
              time: ret.data.data.time,
              file_name: ret.data.data.file_name,
              file_size: ret.data.data.file_size
            }),
            toView: "msg" + that.data.lastId,
            lastId: that.data.lastId + 1
          })
        }


        if (ret.data.data.type == 'image') {
          let imgList = that.data.imgList
          imgList.push(ret.data.data.msg)
          that.setData({
            imgList: imgList
          })
        }


      }
    })
  },
  onShow: function () {
    var that = this
    if (!socketOpen) {
      this.webSocket()
    }
  },
// 建立socket
  webSocket: function () {
    var that = this


    // 先关闭
    // wx.onSocketOpen(function() {
    //   wx.closeSocket()
    // })


    // 创建Socket
    SocketTask = wx.connectSocket({
      url: app.wssurl,
      data: {},
      header: {
        'content-type''application/json'
      },
      method: 'post',
      success: function (res) {
        console.log('WebSocket连接创建', res)
      },
      fail: function (err) {
        wx.showToast({
          title: '网络异常!',
        })
        console.log(err)
      },
    })
  },
  // 销毁页面事件
  onUnload(){
    // 先关闭
    wx.closeSocket()
  }
回答关注问题邀请回答
收藏

1 个回答

登录 后发表内容
问题标签