Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use connectsocket

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you the relevant knowledge of how to use connectsocket. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Wx.connectSocket (OBJECT)

Create a WebSocket connection; a WeChat Mini Programs can only have one WebSocket connection at a time. If a WebSocket connection already exists, it will be automatically closed and a WebSocket connection will be recreated.

OBJECT parameter description:

Parameter type is required to indicate that the minimum version of urlString is the developer server interface address and must be wss protocol, and the domain name must be a legitimate domain name configured at the backend. DataObject No requested data headerObject No HTTP Header. ReferermethodString cannot be set in header. Default is GET. Valid values: OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECTprotocolsStringArray No Sub-Protocol Array 1.4.0successFunction No API call successful callback function failFunction No API call failed callback function completeFunction No API call end callback function (both successful and failed calls will be executed)

Sample code:

Wx.connectSocket ({url: 'test.php', data: {x:', y:'}, header: {'content-type':' application/json'}, protocols: ['protocol1'], method: "GET"}) wx.onSocketOpen (CALLBACK)

Listens for WebSocket connection open events.

Sample code:

Wx.connectSocket ({url: 'test.php'}) wx.onSocketOpen (function (res) {console.log (' WebSocket connection is open!') }) wx.onSocketError (CALLBACK)

Monitor WebSocket error.

Sample code:

Wx.connectSocket ({url: 'test.php'}) wx.onSocketOpen (function (res) {console.log (' WebSocket connection is open!') }) wx.onSocketError (function (res) {console.log ('WebSocket connection failed to open, please check!') }) wx.sendSocketMessage (OBJECT)

To send data through a WebSocket connection, you need to wx.connectSocket first and send it only after the wx.onSocketOpen callback.

OBJECT parameter description:

The parameter type is required to indicate that dataString/ArrayBuffer is the content to be sent. SuccessFunction No API calls successful callback function failFunction No API call failed callback function completeFunction No API call ends callback function (both successful and failed calls will be executed)

Sample code:

Var socketOpen = falsevar socketMsgQueue = [] wx.connectSocket ({url: 'test.php'}) wx.onSocketOpen (function (res) {socketOpen = true for (var I = 0; I < socketMsgQueue.length; iQueue +) {sendSocketMessage (socketOpen) {wx.sendSocketMessage (msg)} wx.onSocketMessage (CALLBACK))

Listens for message events received by the WebSocket to the server.

CALLBACK return parameters:

The parameter type describes the message returned by the dataString/ArrayBuffer server

Sample code:

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

Close the WebSocket connection.

The parameter type is required to indicate whether the minimum version of codeNumber has a numeric value indicating the status number of the closed connection, indicating the reason why the connection was closed. If this parameter is not specified, the default value is 1000 (indicates that the normal connection is closed) 1.4.0reasonString No a readable string indicating the reason why the connection was closed. This string must be UTF-8 text (not characters) no longer than 123bytes. 1.4.0successFunction No API calls successful callback function failFunction No API calls failed callback function completeFunction No API calls the finished callback function (both successful and failed calls will be executed) wx.onSocketClose (CALLBACK)

Listen. WebSocket is off.

Wx.connectSocket ({url: 'test.php'}) / / Note that there is a timing problem here. / / if wx.connectSocket does not call back wx.onSocketOpen and calls wx.closeSocket first, then the purpose of shutting down WebSocket cannot be achieved. / / wx.closeSocket must be called while WebSocket is open to close. Wx.onSocketOpen (function () {wx.closeSocket ()}) wx.onSocketClose (function (res) {console.log ('WebSocket is closed!') }) Bug & Tip

Tip: both the default and maximum timeout for createSocket links are 60s

Tip: the referer of a network request cannot be set, and the format is fixed as https://servicewechat.com/{appid}/{version}/page-frame.html, where {appid} is the appid of Mini Program, {version} is the version number of Mini Program, and a version number of 0 indicates the development version.

These are all the contents of the article "how to use connectsocket". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report