The usage of this API is as follows: wx.sendSocketMessage(Object object)
Attributes | Types | Default value | Required | Note |
data | string/ArrayBuffer | - | Supported | Content to be dispatched |
success | function | - | Not required | Callback function for successful interface invocation |
fail | function | - | Not required | Callback function for unsuccessful interface invocation |
complete | function | - | Not required | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
let socketOpen = falselet socketMsgQueue = []wx.connectSocket({url: 'test.php'})wx.onSocketOpen(function(res) {socketOpen = truefor (let i = 0; i < socketMsgQueue.length; i++){sendSocketMessage(socketMsgQueue[i])}socketMsgQueue = []})function sendSocketMessage(msg) {if (socketOpen) {wx.sendSocketMessage({data:msg})} else {socketMsgQueue.push(msg)}}
The usage method for this API is wx.onSocketOpen(function listener)
Attributes | Types | Note |
header | object | HTTP response Header upon successful connection |
The usage method for this API is wx.onSocketMessage(function listener)
Attributes | Types | Note |
data | string/ArrayBuffer | Message returned by the server |
The usage method for this API is wx.onSocketError(function listener)
Attributes | Types | Note |
errMsg | string | Error Message |
The usage method for this API is wx.onSocketClose(function listener)
Attributes | Types | Note |
code | number | A numerical value representing the status number of the closed connection, indicating the reason for the connection closure. |
reason | string | A readable string indicating the reason for the connection closure. |
The usage method for this API is SocketTask wx.connectSocket(Object object)
Attributes | Types | Default value | Required | Note |
url | string | - | Supported | Developer server's wss interface address |
header | Object | - | Not required | HTTP Header, the Referer cannot be set in the Header. |
protocols | Array.<string> | - | Not required | Array of sub-protocols. |
tcpNoDelay | boolean | false | Not required | TCP_NODELAY setting during the establishment of a TCP connection. |
timeout | number | - | Not required | Timeout duration, measured in milliseconds. |
success | function | - | Not required | Callback function for successful interface invocation |
fail | function | - | Not required | Callback function for unsuccessful interface invocation |
complete | function | - | Not required | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
wx.connectSocket({url: 'wss://example.qq.com',header:{'content-type': 'application/json'},protocols: ['protocol1']})
The usage of this API is wx.closeSocket(Object object).
Attributes | Types | Default value | Required | Note |
code | number | 1000 (indicating a normal closure of the connection) | Not required | A numerical value representing the status number of the closed connection, indicating the reason for the connection closure. |
reason | string | - | Not required | A readable string indicating the reason for the connection closure. This string must be a UTF-8 text that does not exceed 123 bytes in length (not characters). |
success | function | - | Not required | Callback function for successful interface invocation |
fail | function | - | Not required | Callback function for unsuccessful interface invocation |
complete | function | - | Not required | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
wx.connectSocket({url: 'test.php'})//Please note that there may be timing issues here,//If wx.connectSocket has not yet called back wx.onSocketOpen, and wx.closeSocket is called first, then the purpose of closing the WebSocket cannot be achieved.//It is imperative to call wx.closeSocket during the WebSocket's open period to effectuate closure.wx.onSocketOpen(function() {wx.closeSocket()})wx.onSocketClose(function(res) {console.log('WebSocket has been closed!')})
The method is utilized in the form of SocketTask.close(Object object).
Attributes | Types | Default value | Required | Note |
code | number | 1000 (indicating a normal closure of the connection) | Not required | A numerical value representing the status number of the closed connection, indicating the reason for the connection closure. |
reason | string | - | Not required | A readable string indicating the reason for the connection closure. This string must be a UTF-8 text that does not exceed 123 bytes in length (not characters). |
success | function | - | Not required | Callback function for successful interface invocation |
fail | function | - | Not required | Callback function for unsuccessful interface invocation |
complete | function | - | Not required | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
The method is employed as SocketTask.onClose(function listener).
Attributes | Types | Note |
code | number | A numerical value representing the status number of the closed connection, indicating the reason for the connection closure. |
reason | string | A readable string indicating the reason for the connection closure. |
The method is invoked as SocketTask.onError(function listener).
Attributes | Types | Note |
errMsg | string | Error Message |
The method is implemented as SocketTask.onMessage(function listener).
Attributes | Types | Note |
data | string/ArrayBuffer | Message returned by the server |
The method is executed as SocketTask.onOpen(function listener).
Attributes | Types | Note |
header | Object | HTTP response Header upon successful connection |
The usage of this method is SocketTask.send(Object object).
Attributes | Types | Default value | Required | Note |
data | string/ArrayBuffer | - | Supported | Content to be dispatched |
success | function | - | Not required | Callback function for successful interface invocation |
fail | function | - | Not required | Callback function for unsuccessful interface invocation |
complete | function | - | Not required | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
Was this page helpful?