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

WeChat Mini Program Network API Websocket Detailed Description

Mar 13, 2017 am 11:43 AM

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"
})
Copy after login

wx.onSocke

tOpen(CALLBACK)Listen to WebSocket connection opening

Event


Sample code:


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

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

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:
TypeRequiredDescription
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)
 }
}
Copy after login


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)
})
Copy after login

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 已关闭!")
})
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)