title: Taro.sendSocketMessage(option)
sidebar_label: sendSocketMessage
通过 WebSocket 连接发送数据。需要先 Taro.connectSocket,并在 Taro.onSocketOpen 回调之后才能发送。
支持情况:
.default})
类型
(option: Option) => Promise<TaroGeneral.CallbackResult>
参数
| 参数 | 类型 |
|---|---|
| option | Option |
Option
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| data | string or ArrayBuffer | 是 | 需要发送的内容 |
| complete | (res: TaroGeneral.CallbackResult) => void |
否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
| fail | (res: TaroGeneral.CallbackResult) => void |
否 | 接口调用失败的回调函数 |
| success | (res: TaroGeneral.CallbackResult) => void |
否 | 接口调用成功的回调函数 |
示例代码
let socketOpen = falseconst socketMsgQueue = []Taro.connectSocket({url: 'test.php'})Taro.onSocketOpen(function(res) {socketOpen = truefor (let i = 0; i < socketMsgQueue.length; i++){sendSocketMessage(socketMsgQueue[i])}socketMsgQueue = []})function sendSocketMessage(msg) {if (socketOpen) {Taro.sendSocketMessage({data:msg})} else {socketMsgQueue.push(msg)}}
