title: Taro.sendSocketMessage(option)
sidebar_label: sendSocketMessage
通过 WebSocket 连接发送数据。需要先 Taro.connectSocket,并在 Taro.onSocketOpen 回调之后才能发送。
类型
(option: Option) => Promise<CallbackResult>
参数
Option
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| data | string | ArrayBuffer |
是 | 需要发送的内容 |
| complete | (res: CallbackResult) => void |
否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
| fail | (res: CallbackResult) => void |
否 | 接口调用失败的回调函数 |
| success | (res: 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)}}
API 支持度
| API | 微信小程序 | 百度小程序 | 支付宝小程序 | H5 | React Native |
|---|---|---|---|---|---|
| Taro.sendSocketMessage | ✔️ | ✔️ | ✔️ |
