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

Key knowledge summary of Vue development guide

Table of contents Overview 0. JavaScript and Web ...

CSS3 realizes the animation of shuttle starry sky

Result: html <canvas id="starfield"&...

Detailed explanation of the use of custom parameters in MySQL

MySQL variables include system variables and syst...

A brief discussion on the implementation principle of Webpack4 plugins

Table of contents Preface know Practice makes per...

Solution - BASH: /HOME/JAVA/JDK1.8.0_221/BIN/JAVA: Insufficient permissions

1) Enter the folder path where the jdk file is st...

Solve the problem of ugly blue border after adding hyperlink to html image img

HTML img produces an ugly blue border after addin...

VMware virtual machine installation Linux system graphic tutorial

This article shares the specific steps of VMware ...

Detailed explanation of Linux text editor Vim

Vim is a powerful full-screen text editor and the...

Example of using UserMap in IMG

usemap is an attribute of the <img> tag, use...

How to use ElementUI pagination component Pagination in Vue

The use of ElementUI paging component Pagination ...

MySQL UNION operator basic knowledge points

MySQL UNION Operator This tutorial introduces the...

HTTP return code list (Chinese and English explanation)

http return code list (below is an overview) for ...

Detailed explanation of padding and abbreviations within the CSS box model

As shown above, padding values ​​are composite at...

You may not know these things about Mysql auto-increment id

Introduction: When using MySQL to create a table,...