# wx.sendSocketMessage(Object object)

Sends data over WebSocket connection. Call wx.connectSocket first and send data after the wx.onSocketOpen callback.

# Parameters

# Object object

Attribute Type Default Required Description
data string/ArrayBuffer Yes The data to be sent
success function No The callback function for a successful API call
fail function No Callback function for failed API call
complete function No Callback function used when API call completed (always executed whether call succeeds or fails)

# Sample Code

let socketOpen = false
const socketMsgQueue = []
wx.connectSocket({
  url: 'test.php'
})

wx.onSocketOpen(function(res) {
  socketOpen = true
  for (let i = 0; i < socketMsgQueue.length; i++){
    sendSocketMessage(socketMsgQueue[i])
  }
  socketMsgQueue = []
})

function sendSocketMessage(msg) {
  if (socketOpen) {
    wx.sendSocketMessage({
      data:msg
    })
  } else {
    socketMsgQueue.push(msg)
  }
}
点击咨询小助手