跳到主要内容
版本:3.x

Taro.sendSocketMessage(option)

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

支持情况:

参考文档

类型

(option: Option) => Promise<TaroGeneral.CallbackResult>

参数

参数类型
optionOption

Option

参数类型必填说明
datastring or ArrayBuffer需要发送的内容
complete(res: TaroGeneral.CallbackResult) => void接口调用结束的回调函数(调用成功、失败都会执行)
fail(res: TaroGeneral.CallbackResult) => void接口调用失败的回调函数
success(res: TaroGeneral.CallbackResult) => void接口调用成功的回调函数

示例代码

let socketOpen = false
const socketMsgQueue = []
Taro.connectSocket({
url: 'test.php'
})
Taro.onSocketOpen(function(res) {
socketOpen = true
for (let i = 0; i < socketMsgQueue.length; i++){
sendSocketMessage(socketMsgQueue[i])
}
socketMsgQueue = []
})
function sendSocketMessage(msg) {
if (socketOpen) {
Taro.sendSocketMessage({
data:msg
})
} else {
socketMsgQueue.push(msg)
}
}
选择下列对应的群,使用微信扫码添加,会收到入群二维码,再扫群码添加即可。