Home >WeChat Applet >Mini Program Development >WeChat Mini Program Network API Websocket Detailed Description

WeChat Mini Program Network API Websocket Detailed Description

高洛峰
高洛峰Original
2017-03-13 11:43:142701browse

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:

##Parameter TypeRequiredDescriptionurlYesDeveloper dataObjectNoRequested dataheaderObject NoHTTP HeadermethodStringNoThe default is GET, valid values ​​are: OPTIONS, GET, HEAD, POST, PUT, successFunctionNoCallback function for successful interface callfailcompleteSample code:
String ServiceServerInterfaceThe address must be HTTPS protocol, and the domain name must be the legal domain name configured in the background
DELETE, TRACE, CONNECT
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"
})

wx.onSocke

tOpen(CALLBACK)Listen to WebSocket connection opening

Event


Sample code:


wx.connectSocket({
 url:"test.php"
});
wx.onSocketOpen(function(res){
 console.log("WebSocket连接已打开!")
})

wx.onSocketError(CALLBACK)

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连接打开失败,请检查!")
})

wx.s

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:



ParameterdataSample code:
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:


ParametersTypeDescriptiondataStringMessage returned by the serverSample code:


##

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn