title: Taro.sendSocketMessage(option)
sidebar_label: sendSocketMessage
Sends data over WebSocket connection. Call Taro.connectSocket first and send data after the Taro.onSocketOpen callback.
Type
(option: Option) => Promise<CallbackResult>
Parameters
Option
| Property | Type | Required | Description |
|---|---|---|---|
| data | string | ArrayBuffer |
Yes | The data to be sent |
| complete | (res: CallbackResult) => void |
No | The callback function used when the API call completed (always executed whether the call succeeds or fails) |
| fail | (res: CallbackResult) => void |
No | The callback function for a failed API call |
| success | (res: CallbackResult) => void |
No | The callback function for a successful API call |
Sample Code
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 Support
| API | WeChat Mini-Program | Baidu Smart-Program | Alipay Mini-Program | H5 | React Native |
|---|---|---|---|---|---|
| Taro.sendSocketMessage | ✔️ | ✔️ | ✔️ |
