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. |
perMessageDeflate | boolean | false | Not required | Enablement of compression extension. |
timeout | number | - | Not required | Timeout duration, measured in milliseconds. |
forceCellularNetwork | boolean | false | Not required | Sending requests via mobile network while connected to wifi. |
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 |
profile | Object | Some debugging information during the network request process. |
Structural Attributes | Types | Note |
fetchStart | number | The component is prepared to use SOCKET to initiate a request, which occurs prior to the inspection of the local cache. |
domainLookupStart | number | The time at which the DNS domain name query begins. If the local cache is utilized (i.e., no DNS query) or a persistent connection is established, it is equivalent to the fetchStart value. |
domainLookupEnd | number | The time at which the DNS domain name query is completed. If the local cache is utilized (i.e., no DNS query) or a persistent connection is established, it is equivalent to the fetchStart value. |
connectStart | number | The time at which the connection begins to be established. If it is a persistent connection, it corresponds to the fetchStart value. Note that if an error occurs at the transport layer and the connection is reestablished, the time displayed here is the start time of the newly established connection. |
connectEnd | number | The time at which the connection is fully established (handshake completed). If it is a persistent connection, it corresponds to the fetchStart value. Note that if an error occurs at the transport layer and the connection is reestablished, the time displayed here is the completion time of the newly established connection. Note that the end of the handshake here includes the completion of the secure connection establishment and the approval of SOCKS authorization. |
rtt | number | The duration of a single connection, encompassing both the connect and tls phases. |
handshakeCost | number | Handshake duration. |
cost | number | The duration from the upper layer request to the response. |
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?