wx.sendSocketMessage(Object object)

通过 WebSocket 连接发送数据。需要先 wx.connectSocket,并在 wx.onSocketOpen 回调之后才能发送。

参数

Object object

属性类型默认值必填说明
datastring/ArrayBuffer需要发送的内容
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

示例代码

  1. let socketOpen = false
  2. let socketMsgQueue = []
  3. wx.connectSocket({
  4. url: 'test.php'
  5. })
  6. wx.onSocketOpen(function(res) {
  7. socketOpen = true
  8. for (let i = 0; i < socketMsgQueue.length; i++){
  9. sendSocketMessage(socketMsgQueue[i])
  10. }
  11. socketMsgQueue = []
  12. })
  13. function sendSocketMessage(msg) {
  14. if (socketOpen) {
  15. wx.sendSocketMessage({
  16. data:msg
  17. })
  18. } else {
  19. socketMsgQueue.push(msg)
  20. }
  21. }