vue使用websocket連接優(yōu)化性能方式
使用websocket連接優(yōu)化性能
需求
前端做echarts 圖表展示,每隔五秒鐘刷新一次數(shù)據(jù)
問題
前期用的是axios 輪詢,添加定時器每秒請求一次接口,出現(xiàn)卡頓,服務(wù)器負(fù)擔(dān)大現(xiàn)象,且不同人打開可能會顯示不同的數(shù)據(jù)
解決
使用了websocket建立長連接
(websocket只需要一次HTTP握手,所以說整個通訊過程是建立在一次連接/狀態(tài)中,也就避免了HTTP的非狀態(tài)性,服務(wù)端會一直知道你的信息,直到你關(guān)閉請求)
data(){ WsUrl:'',//網(wǎng)址 lock:false//重復(fù)鏈接標(biāo)識 url:'', token:'', deviceid:'', reconnetTimeout:null, timer:null, serverTimer:null, timeout:10*1000 } mounted () { this.createWebsocket() }, methods:{ createWebsocket(){ try{ initWebSocket() }catch{ reConnect() } } initWebSocket () { // 創(chuàng)建一個構(gòu)造函數(shù)返回一個websocket對象 this.wsurl = `ws://${this.url}${this.types}${this.token}?deviceid=${this.deviceid}` this.websock = new WebSocket(wsurl) // 接受到信息后的回調(diào)函數(shù) this.websock.onmessage = this.websocketonmessage // 連接成功后的回調(diào)函數(shù) this.websock.onopen = this.websocketonopen // 連接失敗后的回調(diào)函數(shù) this.websock.onerror = this.websocketonerror // 用于指定關(guān)閉后的回調(diào)函數(shù) this.websock.onclose = this.websocketclose }, // 連接建立失敗重連 websocketonerror () { this.reConnet() }, websocketonopen(){ this.start(this.websocket) this.websocketSend() } // 數(shù)據(jù)接收 websocketonmessage (e) { // 處理業(yè)務(wù)邏輯 if(e.data==‘pong'){ this.statr(this.websock) } } // 關(guān)閉 websocketclose (e) { this.reConnet() }, } reConnect() { if(this.lock) { return; }; this.lock = true; //沒連接上會一直重連,設(shè)置延遲避免請求過多 this.reconnectTime&& clearTimeout(this.reconnectTime); this.reconnectTime= setTimeout(function () { this.createWebSocket(); this.lock = false; }, 4000); } WebsocketSend(){ this.websock.send(‘ping') } start(ws){ this.serverTimer&&clearTimeout(this.serverTime) this.timer&&clearTimeout(this.timer) this.timer=setTimeout(()=>{ ws.send(‘ping') this.serverTimer=setTimeout(()=>{ ws.close() },this.timeout}; },this.timeout) }
websocket在vue上的使用
<template> <div> <button @click="send">發(fā)消息</button> </div> </template> <script> export default { data () { return { path:"ws://192.168.1.145:8080", socket:"" } }, mounted () { // 初始化 this.init() }, methods: { init: function () { if(typeof(WebSocket) === "undefined"){ alert("您的瀏覽器不支持socket") }else{ // 實(shí)例化socket this.socket = new WebSocket(this.path) // 監(jiān)聽socket連接 this.socket.onopen = this.open // 監(jiān)聽socket錯誤信息 this.socket.onerror = this.error // 監(jiān)聽socket消息 this.socket.onmessage = this.getMessage } }, open: function (res) { console.log("socket連接成功") }, error: function () { console.log("連接錯誤") }, getMessage: function (msg) { /*數(shù)據(jù)*/ console.log(msg.data) }, /* send: function () { this.socket.send(params) },*/ close: function () { console.log("socket已經(jīng)關(guān)閉") } }, destroyed () { // 銷毀監(jiān)聽 this.socket.onclose = this.close } } </script> <style> </style>
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue使用 onMounted 確保在組件掛載后執(zhí)行異步操作示例詳解
在 Vue.js 或其他類似框架中,使用 onMounted 是為了確保在組件掛載后執(zhí)行異步操作,這篇文章主要介紹了Vue使用onMounted確保在組件掛載后執(zhí)行異步操作,需要的朋友可以參考下2023-06-06vue3父子同信的雙向數(shù)據(jù)的項(xiàng)目實(shí)現(xiàn)
我們知道的是,父傳子的通信,和子傳父的通信,那如何實(shí)現(xiàn)父子相互通信的呢,本文就來詳細(xì)的介紹一下,感興趣的可以了解一下2023-08-08vue input輸入框關(guān)鍵字篩選檢索列表數(shù)據(jù)展示
這篇文章主要為大家詳細(xì)介紹了vue input輸入框關(guān)鍵字篩選檢索列表數(shù)據(jù)展示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05Vue的export?default和帶返回值的data()及@符號的用法說明
這篇文章主要介紹了Vue的export?default和帶返回值的data()及@符號的用法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03