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

The complete usage of setup, ref, and reactive in Vue3 combination API

1. Getting started with setUp Briefly introduce t...

Implementation of Mysql User Rights Management

1. Introduction to MySQL permissions There are 4 ...

Detailed explanation of the process of modifying Nginx files in centos7 docker

1. Install nginx in docker: It is very simple to ...

Use of kubernetes YAML files

Table of contents 01 Introduction to YAML files Y...

MySql index improves query speed common methods code examples

Use indexes to speed up queries 1. Introduction I...

CSS code to distinguish ie8/ie9/ie10/ie11 chrome firefox

Website compatibility debugging is really annoyin...

Sharing several methods to disable page caching

Today, when developing, I encountered a method wh...

How to optimize MySQL deduplication operation to the extreme

Table of contents 1. Clever use of indexes and va...

How to implement the King of Glory matching personnel loading page with CSS3

Those who have played King of Glory should be fam...

How to monitor oracle database using zabbix agent2

Overview In zabbix version 5.0 and above, a new f...

A brief discussion on the solution of Tomcat garbled code and port occupation

Tomcat server is a free and open source Web appli...

How to run MySQL using docker-compose

Directory Structure . │ .env │ docker-compose.yml...

XHTML Web Page Tutorial

This article is mainly to let beginners understan...

mysql three tables connected to create a view

Three tables are connected. Field a of table A co...