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
1. Getting started with setUp Briefly introduce t...
1. Introduction to MySQL permissions There are 4 ...
1. Install nginx in docker: It is very simple to ...
How to implement text icons through CSS /*icon st...
Table of contents 01 Introduction to YAML files Y...
Use indexes to speed up queries 1. Introduction I...
Website compatibility debugging is really annoyin...
Today, when developing, I encountered a method wh...
Table of contents 1. Clever use of indexes and va...
Those who have played King of Glory should be fam...
Overview In zabbix version 5.0 and above, a new f...
Tomcat server is a free and open source Web appli...
Directory Structure . │ .env │ docker-compose.yml...
This article is mainly to let beginners understan...
Three tables are connected. Field a of table A co...