Bootstrap

微信小程序使用连接websocket和调长链接接口

let socketOpen = false
    let socketMsgQueue = []
    wx.connectSocket({ //请求socket
      url: '长链接接口路径',
      success: res => {
        console.log("连接成功")
        console.log(res)
      }
    })
    wx.onSocketOpen(function (res) { //监听socket
      console.log(res)
      socketOpen = true
      for (let i = 0; i < socketMsgQueue.length; i++) {
        sendSocketMessage(socketMsgQueue[i])
      }
	//-----页面内使用长链接接口发送和接收数据时也是如下方法:
      let streamconfig = {
      //这里是后端需要的参数
      }
      //发送数据
      wx.sendSocketMessage({ //
        data: JSON.stringify(streamconfig),
        success: res => {
          console.log('发送数据')
          console.log(res)
        }
      })
      //接收数据
      wx.onSocketMessage(res => {
        console.log(res)
      })

;