May 18, 2021 WeChat Mini Program Development Document
Create a WebSocket connection; a WeChat widget can only have one WebSocket connection at a time, and if a WebSocket connection currently exists, it automatically closes the connection and re-creates a WebSocket connection.
OBJECT parameter description:
Parameters | Type | Required | Description | The lowest version |
---|---|---|---|---|
Url | String | Is | Developer server interface address, must be a wss protocol, and the domain name must be a legitimate domain name configured in the background | |
data | Object | Whether | The requested data | |
header | Object | Whether | HTTP Header, Referer cannot be set in Header | |
method | String | Whether | The default is GET, valid values: OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT | |
protocols | StringArray | Whether | An array of sub-protocols | 1.4.0 |
success | Function | Whether | The interface calls a successful callback function | |
fail | Function | Whether | The interface calls the failed callback function | |
complete | Function | Whether | Callback function at end of interface call (call succeeds, fails are executed) |
wx.connectSocket({
url: 'test.php',
data:{
x: '',
y: ''
},
header:{
'content-type': 'application/json'
},
protocols: ['protocol1'],
method:"GET"
})
Listen for WebSocket connection open events.
Example code:
wx.connectSocket({
url: 'test.php'
})
wx.onSocketOpen(function(res) {
console.log('WebSocket连接已打开!')
})
Listen for WebSocket errors.
Example code:
wx.connectSocket({
url: 'test.php'
})
wx.onSocketOpen(function(res){
console.log('WebSocket连接已打开!')
})
wx.onSocketError(function(res){
console.log('WebSocket连接打开失败,请检查!')
})
Sending data over a WebSocket connection requires wx.connectSocket and a wx.onSocketOpen callback before it can be sent.
OBJECT parameter description:
Parameters | Type | Required | Description |
---|---|---|---|
data | String/ArrayBuffer | Is | What needs to be sent |
success | Function | Whether | The interface calls a successful callback function |
fail | Function | Whether | The interface calls the failed callback function |
complete | Function | Whether | Callback function at end of interface call (call succeeds, fails are executed) |
Example code:
var socketOpen = false
var socketMsgQueue = []
wx.connectSocket({
url: 'test.php'
})
wx.onSocketOpen(function(res) {
socketOpen = true
for (var i = 0; i < socketMsgQueue.length; i++){
sendSocketMessage(socketMsgQueue[i])
}
socketMsgQueue = []
})
function sendSocketMessage(msg) {
if (socketOpen) {
wx.sendSocketMessage({
data:msg
})
} else {
socketMsgQueue.push(msg)
}
}
Listen for message events that WebSocket accepts to the server.
CALLBACK returns parameters:
Parameters | Type | Description |
---|---|---|
data | String/ArrayBuffer | The message returned by the server |
Example code:
wx.connectSocket({
url: 'test.php'
})
wx.onSocketMessage(function(res) {
console.log('收到服务器内容:' + res.data)
})
Close the WebSocket connection.
Parameters | Type | Required | Description | The lowest version |
---|---|---|---|---|
code | Number | Whether | A numeric value represents the status number of the closed connection, indicating why the connection was closed. If this parameter is not specified, the default value is 1000 (indicating that the normal connection is closed) | 1.4.0 |
reason | String | Whether | A readable string that indicates why the connection was closed. This string must be no longer than 123 bytes of UTC-8 text (not characters) | 1.4.0 |
success | Function | Whether | The interface calls a successful callback function | |
fail | Function | Whether | The interface calls the failed callback function | |
complete | Function | Whether | Callback function at end of interface call (call succeeds, fails are executed) |
Listen to WebSocket off.
wx.connectSocket({
url: 'test.php'
})
//注意这里有时序问题,
//如果 wx.connectSocket 还没回调 wx.onSocketOpen,而先调用 wx.closeSocket,那么就做不到关闭 WebSocket 的目的。
//必须在 WebSocket 打开期间调用 wx.closeSocket 才能关闭。
wx.onSocketOpen(function() {
wx.closeSocket()
})
wx.onSocketClose(function(res) {
console.log('WebSocket 已关闭!')
})
Return value:
Base library 1.7.0 starts to support, and low versions need to be compatible
Return a SocketTask.
tip
: CreateSocket link default and maximum timeout are 60s
tip
The referer requested by the network can not be set, the format is fixed to https://servicewechat.com/ 'appid'/'version'/page-frame.html, where the
{appid}
of the applet, the version
{version}
and the version number 0 is the development version.
Base library 1.7.0 starts to support, and low versions need to be compatible
WebSocket tasks, which can be created through the wx.connectSocket() interface.
Send data over a WebSocket connection.
OBJECT parameter description:
Parameters | Type | Required | Description |
---|---|---|---|
data | String/ArrayBuffer | Is | What needs to be sent |
success | Function | Whether | The interface calls a successful callback function |
fail | Function | Whether | The interface calls the failed callback function |
complete | Function | Whether | Callback function at end of interface call (call succeeds, fails are executed) |
Turn off the WebSocket connection.
OBJECT parameter description:
Parameters | Type | Required | Description |
---|---|---|---|
code | Number | Whether | A numeric value represents the status number of the closed connection, indicating why the connection was closed. If this parameter is not specified, the default value is 1000 (indicating that the normal connection is closed) |
reason | String | Whether | A readable string that indicates why the connection was closed. This string must be no longer than 123 bytes of UTC-8 text (not characters) |
success | Function | Whether | The interface calls a successful callback function |
fail | Function | Whether | The interface calls the failed callback function |
complete | Function | Whether | Callback function at end of interface call (call succeeds, fails are executed) |
Listen for WebSocket connection open events.
Listen for WebSocket connection shutdown events.
Listen for WebSocket errors.
CALLBACK returns parameters:
Parameters | Type | Description |
---|---|---|
errMsg | String | The error message |
Listen for message events that WebSocket accepts to the server.
CALLBACK returns parameters:
Parameters | Type | Description |
---|---|---|
data | String/ArrayBuffer | The message returned by the server |