APIWebsocket on WeChat applet network

Source: Internet
Author: User
This article mainly introduces detailed information about the mini-program network APIWebsocket. if you need it, you can refer to the next article, which mainly introduces detailed information about the mini-program Network API Websocket, for more information, see

Wx. connectSocket (OBJECT)

Create a WebSocket connection. a applet can only have one WebSocket connection at the same time. if a WebSocket connection already exists, the connection is automatically closed and a WebSocket connection is created again.

OBJECT parameter description:

Parameters Type Required Description
Url String Yes The developer server interface address, which must be an HTTPS protocol and a valid domain name configured in the background
Data Object No Requested Data
Header Object No HTTP Header
Method String No The default value is GET. Valid values include OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, and CONNECT.
Success Function No Callback function for successful API call
Fail Function No Callback function for interface call failure
Complete Function No The callback function after the interface call ends. (the callback function is executed if the call succeeds or fails)

Sample code:


wx.connectSocket({ url:"test.php", data:{ x:"", y:"" }, header:{  'content-type': 'application/json' }, method:"GET"})

Wx. onSocketOpen (CALLBACK)

Listen to WebSocket connection opening events

Sample code:


Wx. connectSocket ({url: "test. php"}); wx. onSocketOpen (function (res) {console. log ("WebSocket connection enabled! ")})

Wx. onSocketError (CALLBACK)

WebSocket listening error

Sample code:


Wx. connectSocket ({url: "test. php"}); wx. onSocketOpen (function (res) {console. log ("WebSocket connection enabled! ")}) Wx. onSocketError (function (res) {console. log (" failed to open WebSocket connection. Please check! ")})

Wx. sendSocketMessage (OBJECT)

To send data through a WebSocket connection, you must first wx. connectSocket and send the data only after the wx. onSocketOpen callback.
OBJECT parameter description:

Parameters Type Required Description
Data String Yes Content to be sent

Sample 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) }}

Wx. onSocketMessage (CALLBACK)

Listen to WebSocket receive message events to the server

CALLBACK return parameters:

Parameters Type Description
Data String Message returned by the server

Sample code:


Wx. connectSocket ({url: "test. php "}); wx. onSocketMessage (function (res) {console. log ("received server content:" + res. data )})

Wx. closeSocket ()

Disable WebSocket connection

Wx. onSocketClose (CALLBACK)

Disable WebSocket listening


Wx. connectSocket ({url: "test. php "}); // note that there is a timing problem, // If wx. wx socket has not called back wx. onSocketOpen, but first call wx. closeSocket, you cannot close WebSocket. // you must call wx when WebSocket is enabled. closeSocket can disable wx. onSocketOpen (function () {wx. closeSocket ()}) wx. onSocketClose (function (res) {console. log ("WebSocket is disabled! ")})

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

The above is the detailed description of the mini-program Network API Websocket. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.