Home >WeChat Applet >Mini Program Development >WeChat Mini Program Network API Websocket Detailed Description
This article mainlyintroduces the relevant information of the WeChat Mini Program NetworkAPI Websocket in detail. Friends in need can refer to
wx. connectSocket(OBJECT)
Create a WebSocket connection; a WeChat applet can only have one WebSocket connection at the same time. If a WebSocket connection currently exists, the connection will be automatically closed. and re-create a WebSocket connection.
OBJECT parameter description:
Type | Required | Description | |
---|---|---|---|
String | YesDeveloper | ServiceServerInterfaceThe address must be HTTPS protocol, and the domain name must be the legal domain name configured in the background | |
Object | No | Requested data | |
Object | No | HTTP Header | |
String | No | The default is GET, valid values are: OPTIONS, GET, HEAD, POST, PUT, | DELETE, TRACE, CONNECT |
Function | No | Callback function for successful interface call | |
Function | No | Interface The callback function that failed to call | |
Function | No | The callback function that ends the interface call (either successful or failed) Execution) |
wx.connectSocket({ url:"test.php", data:{ x:"", y:"" }, header:{ 'content-type': 'application/json' }, method:"GET" })
tOpen(CALLBACK)Listen to WebSocket connection opening
Eventwx.connectSocket({ url:"test.php" }); wx.onSocketOpen(function(res){ console.log("WebSocket连接已打开!") })
Listen for WebSocket errors
Sample code:
wx.connectSocket({ url:"test.php" }); wx.onSocketOpen(function(res){ console.log("WebSocket连接已打开!") }) wx.onSocketError(function(res){ console.log("WebSocket连接打开失败,请检查!") })
endSocketMessage(OBJECT)To send data through a WebSocket connection, you need to first wx.connectSocket, and can only be sent after wx.onSocketOpen callback.
OBJECT parameter description:Type | Required | Description | |
---|---|---|---|
String | is the content that | needs to be sent |
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) } }
wx.onSocketMessage(CALLBACK)
Listening to WebSocket received Server message event
CALLBACK return parameters:
Description | ||
---|---|---|
Message returned by the server |
##
wx.connectSocket({ url:"test.php" }); wx.onSocketMessage(function(res){ console.log("收到服务器内容:" + res.data) })
wx.closeSocket()
Close WebSocket connection
wx.onSocketClose(CALLBACK)
Listen for WebSocket close
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 已关闭!") })Thanks for reading , hope it can help everyone, thank you for your support of this site!
The above is the detailed content of WeChat Mini Program Network API Websocket Detailed Description. For more information, please follow other related articles on the PHP Chinese website!