Real-time refresh of long connection on Vue+WebSocket page

Real-time refresh of long connection on Vue+WebSocket page

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 use webSocket to update real-time weather in Vue
  • A brief discussion on the pitfalls of real-time communication using vue websocket nodeJS
  • Vue implements websocket to send messages and receive messages in real time
  • Websocket+Vuex implements a real-time chat software

<<:  How to add, delete and modify columns in MySQL database

>>:  Implementation of CentOS8.0 network configuration

Recommend

Detailed explanation of setting resource cache in nginx

I have always wanted to learn about caching. Afte...

An article to deal with Mysql date and time functions

Table of contents Preface 1. Get the current time...

Detailed explanation of MySQL file storage

What is a file system We know that storage engine...

The reason why MySQL uses B+ tree as its underlying data structure

We all know that the underlying data structure of...

Some questions about hyperlinks

I am very happy to attend this episode of potato ...

jQuery plugin to implement minesweeper game (1)

This article shares the specific code of the firs...

Analyze the selection problem of storing time and date types in MySQL

In general applications, we use timestamp, dateti...

MySQL 5.7.31 64-bit free installation version tutorial diagram

1. Download Download address: https://dev.mysql.c...

SQL to implement time series dislocation restoration case

Table of contents 1. Requirements description 2. ...

How to use Font Awesome 5 in Vue development projects

Table of contents Install Dependencies Configurat...

An article tells you how to implement Vue front-end paging and back-end paging

Table of contents 1: Front-end handwritten paging...

Mysql command line mode access operation mysql database operation

Usage Environment In cmd mode, enter mysql --vers...

What is ZFS? Reasons to use ZFS and its features

History of ZFS The Z File System (ZFS) was develo...

Vue page monitoring user preview time function implementation code

A recent business involves such a requirement tha...