In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "how to apply Mini Program websocket heartbeat database", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Mini Program websocket heartbeat database how to use" article.
Introduction
Websocket-heartbeat-miniprogram is encapsulated based on Mini Program's websocket-related API, and its main purpose is to ensure the connection status between the client websocket and the server. The program has heartbeat detection and automatic reconnection mechanism. When the network is disconnected or the back-end service problem causes the client websocket to disconnect, the program will automatically try to reconnect until the connection is successful. Compatible with most Mini Program Wechat, Baidu, Alipay, etc., as long as they are unified Mini Program weboscket-API specifications. Mini Program frameworks such as Taro are also supported. In any case, the business needs a heartbeat mechanism, otherwise the connection will be lost in some cases and the function will not be used.
Usage
Installation
Npm install-save websocket-heartbeat-miniprogram
Introduction and use
Import WebsocketHeartbeat from 'websocket-heartbeat-miniprogram';WebsocketHeartbeat ({miniprogram: wx, connectSocketParams: {url:' ws://xxx'}}). Then (task = > {task.onOpen = () = > {/ / hook function console.log ('open');}; task.onClose = () = > {/ / hook function console.log (' close');} Task.onError = e = > {/ / hook function console.log ('onError:', e);}; task.onMessage = data = > {/ / hook function console.log (' onMessage', data);}; task.onReconnect = () = > {/ / hook function console.log ('reconnect...');} Task.socketTask.onOpen (data = > {/ / native instance registration function, lose console.log ('socketTask open') after reconnection;}); task.socketTask.onMessage (data = > {/ / native instance registration function, lose console.log (' socketTask data');});})
Within this procedure, the Mini Program connectSocket method is always used for socket connection. If the socket is disconnected, the internal Mini Program connectSocket method will be executed again to re-establish the connection, and a new Mini Program socket instance will be established if the connection is reconnected.
The WebsocketHeartbeat method returns a promise, and the returned task object is an instance of the program, providing hook functions such as onOpen,onClose,onError,onMessage,onReconnect. The socketTask of Mini Program itself is also exposed. Task.socketTask is the instance returned by Mini Program connectSocket, and task.socketTask is the native instance of Mini Program, which listens and registers through the transfer function of onXXX method. After socket is reconnected, a new instance is created internally through connectSocket, and new Mini Program native instance will be returned, so these functions previously registered through socketTask.onXXX will be lost. The hook function provided within this program is still valid after being reconnected. In most cases, it is recommended to use the hook function of this program.
Alipay Mini Program differences
Alipay Mini Program only allows one socket connection at the same time, and the native API is also a little different from other Mini Program. This procedure is compatible, but you should note that Alipay only allows you to establish one socket. If you establish multiple socket, the previous socket will be closed by the system. Be sure to close the old socket through the task.close of this program instance, otherwise the program will always be reconnected, resulting in all socket conflicts and cannot be used.
Agreement
1. You can only actively close the socket connection through the front end.
If you need to disconnect the socket, you should execute the task.close () method. If the backend wants to shut down socket, it should send a message, the front end judges the message, and the front end calls the task.close () method to close it. Because whether the backend calls close or the socket shuts down due to other reasons, the front-end socket will trigger the onClose event, and the program cannot determine what caused the shutdown. Therefore, this program will try to reconnect by default.
Import WebsocketHeartbeat from 'websocket-heartbeat-miniprogram';WebsocketHeartbeat ({miniprogram: wx, connectSocketParams: {url:' ws://xxxx'}}) .then (task = > {task.onMessage = data = > {if (data.data = = 'close') task.close (); / / close socket and, no longer reconnect};})
two。 Back-end feedback to front-end heartbeat
The front end sends the heartbeat message, and the back end needs to return the response message immediately after receiving it. The response message of the back end can be any value, because this program does not process and judge the heartbeat message of the response, but only resets the heartbeat after receiving any message, because receiving any message means that the connection is normal. Therefore, this program will reset the heartbeat countdown to any message returned by the back end in order to reduce unnecessary requests and reduce the pressure on the server.
The above is about the content of this article "how to apply Mini Program websocket heartbeat library". I believe you all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please follow 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.