# wx.sendSocketMessage(Object object)
with Promise style call: Supported
Mini Program plugin: Not supported
adopt WebSocket Connection sends data. Need first wx.connectSocket, and the wx.onSocketOpen After the callback can be sent.Recommended use SocketTask The way to manage webSocket Link, the life cycle of each link is more controllable. There are multiple simultaneous webSocket In the case of links to wx Prefixes may bring up some of the situations that are not expected.
# parameter
# Object object
attribute | type | Default values | Required | Introductions |
---|---|---|---|---|
data | string/ArrayBuffer | yes | What to send | |
success | function | no | Interface calls the successful callback function | |
fail | function | no | Interface calls failed callback functions | |
complete | function | no | Callback function at the end of an interface call (both successful and unsuccessful calls are executed) |
# sample code
let socketOpen = false
let socketMsgQueue = []
wx.connectSocket({
url: 'test.php'
})
wx.onSocketOpen(function(res) {
socketOpen = true
for (let i = 0 i < socketMsgQueue.length i++){
sendSocketMessage(socketMsgQueue[i])
}
socketMsgQueue = []
})
function sendSocketMessage(msg) {
if (socketOpen) {
wx.sendSocketMessage({
Data: msg
})
} else {
socketMsgQueue.push(msg)
}
}