Recently, the Vue project needs to refresh the data in real time. The line chart is redrawn every second, and the data is refreshed every 0.5 seconds. To put it simply, it is a real-time refresh. Because the amount of data is large, it is estimated that the page will be stuck if it stays for a while using a timer. . . After discussing with the backend staff, we decided to use the newly added WebSocket of h5 to realize real-time data display, and record the process and problems encountered; Note: The long connection for page refresh will be closed. In fact, the purpose of establishing a long connection when entering the current page is to refresh the page without F5. All data is automatically refreshed in real time. If you still refresh the page back and forth with F5, it will be meaningless. . . ps: If you really need this, there seems to be a way to refresh the page and keep the connection connected. Please search on Baidu. . . . <template> <div> </div> </template> <script> export default { data() { return { websock: null, } }, created(){ //Open a long connection when the page is just entered this.initWebSocket() }, destroyed: function() { //Close the long connection when the page is destroyed this.websocketclose(); }, methods: { initWebSocket(){ //Initialize weosocket const wsuri = process.env.WS_API + "/websocket/threadsocket"; //ws address this.websock = new WebSocket(wsuri); this.websocket.onopen = this.websocketonopen; this.websocket.onerror = this.websocketonerror; this.websock.onmessage = this.websocketonmessage; this.websock.onclose = this.websocketclose; }, websocketonopen() { console.log("WebSocket connection successful"); }, websocketonerror(e) { //Error console.log("WebSocket connection error"); }, websocketonmessage(e){ //Data reception const redata = JSON.parse(e.data); //Note: For long connection, we push one piece of data per second directly in the background. //But when you click on a list, an identifier will be sent to the backend, and the backend will return the corresponding data based on this identifier. //At this time, data can only be sent out from one exit, so a key is added to the background. For example, when the key is 1, data is pushed every 1 second, and when it is 2, data is pushed after sending the mark, for distinction console.log(redata.value); }, websocketsend(agentData){//data sending this.websock.send(agentData); }, websocketclose(e){ //Close console.log("connection closed (" + e.code + ")"); }, }, } </script> This is the end of this article about the implementation of real-time refresh of long connection of Vue+WebSocket page. For more relevant Vue+WebSocket real-time refresh of long connection content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to add, delete and modify columns in MySQL database
>>: Implementation of CentOS8.0 network configuration
I have always wanted to learn about caching. Afte...
Table of contents Preface 1. Get the current time...
What is a file system We know that storage engine...
We all know that the underlying data structure of...
I am very happy to attend this episode of potato ...
This article shares the specific code of the firs...
yum quick install mysql Add yum repository rpm -U...
In general applications, we use timestamp, dateti...
1. Download Download address: https://dev.mysql.c...
Table of contents 1. Requirements description 2. ...
Table of contents Install Dependencies Configurat...
Table of contents 1: Front-end handwritten paging...
Usage Environment In cmd mode, enter mysql --vers...
History of ZFS The Z File System (ZFS) was develo...
A recent business involves such a requirement tha...