search
HomeWeChat AppletMini Program DevelopmentWeChat 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:

##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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool