From 9fc3a1d5a70a5af805b37476d47331dad0b21d65 Mon Sep 17 00:00:00 2001 From: whyczyk <525500596@qq.com> Date: 星期六, 09 十月 2021 15:09:32 +0800 Subject: [PATCH] 模块socket优化请求数据 --- src/assets/js/socket.js | 138 +++++++++++++++++++++++++++------------------ 1 files changed, 82 insertions(+), 56 deletions(-) diff --git a/src/assets/js/socket.js b/src/assets/js/socket.js index 75be6ec..12f9f20 100644 --- a/src/assets/js/socket.js +++ b/src/assets/js/socket.js @@ -1,67 +1,93 @@ import getWsUrl from "@/assets/js/getWsUrl"; -var websock = null; -var global_callback = null; -function initWebSocket(action, port) { //鍒濆鍖杦eosocket +export class WebSocketClass { //ws鍦板潃 - const wsUri = getWsUrl(action, port); - websock = new WebSocket(wsUri); - websock.onmessage = function (e) { - websocketonmessage(e); + constructor(url, msgCallback, time, port) { + this.wsUrl = getWsUrl(url, port) + this.msgCallback = msgCallback; + this.time = time; //蹇冭烦鏃堕棿 + this.initTime = 5000 //閲嶈繛鏃堕棿 + this.init() } - websock.onclose = function (e) { - websocketclose(e); - } - websock.onopen = function () { - websocketOpen(); + init(data) { + let WSINWIN = 'WebSocket' in window + if (!WSINWIN) { + return console.log('Websocket not supported') + } + this.ws = new WebSocket(this.wsUrl); + this.ws.onopen = () => { + this.status = 'open'; + this.heartCheck(); + if (data) { this.ws.send(data) } + }; + + this.ws.onmessage = e => { + if (e.data === 'pong') { + this.pingPong = 'pong' + } else { + this.msgCallback(JSON.parse(e.data)); + } + }; + + this.ws.onclose = () => { + clearInterval(this.pingInterval); + clearInterval(this.pongInterval) + if (this.status === 'close') { + //console.log(鈥樻墜鍔ㄥ叧闂垚鍔熲��,new Date()) + } else { + this.relink(); //闈炰汉涓哄叧闂繘琛岄噸杩� + } + } + this.ws.onerror = e => { + console.log(e); + this.relink() + } + return false } - //杩炴帴鍙戠敓閿欒鐨勫洖璋冩柟娉� - websock.onerror = function () { - console.log("WebSocket杩炴帴鍙戠敓閿欒"); + heartCheck() {// 蹇冭烦 + this.pingPong = 'ping'; + let timer = this.time + 1000 + this.pingInterval = setInterval(() => { + if (this.ws.readyState === 1) { + this.ws.send('ping') + } + }, this.time); + this.pongInterval = setInterval(() => { + if (this.pingPong === 'ping') { + clearInterval(this.pingInterval); + clearInterval(this.pongInterval) + this.ws.close() + } else { + this.pingPong = 'ping' + } + }, timer) } -} -// 瀹為檯璋冪敤鐨勬柟娉� -function sendSock(agentData, callback) { - global_callback = callback; - if (websock.readyState === websock.OPEN) { - //鑻ユ槸ws寮�鍚姸鎬� - websocketsend(agentData) - } else if (websock.readyState === websock.CONNECTING) { - // 鑻ユ槸 姝e湪寮�鍚姸鎬侊紝鍒欑瓑寰�1s鍚庨噸鏂拌皟鐢� - setTimeout(function () { - sendSock(agentData, callback); - }, 1000); - } else { - // 鑻ユ湭寮�鍚� 锛屽垯绛夊緟1s鍚庨噸鏂拌皟鐢� - setTimeout(function () { - sendSock(agentData, callback); - }, 1000); + sendHandle(data) { + return this.ws.send(data); } -} -//鏁版嵁鎺ユ敹 -function websocketonmessage(e) { - global_callback(JSON.parse(e.data)); -} + relink() { + if (this.status == 'open' && this.readyState == 1) { //杩炴帴姝e父 + return + } + if (this.initTimeOut) { //瀹氭椂鍣ㄥ凡缁忓惎鍔� + return; + } + this.initTimeOut = setTimeout(() => { + clearInterval(this.pingInterval); + clearInterval(this.pongInterval); + this.initTimeOut = null; + this.init(); + }, this.initTime) + } -//鏁版嵁鍙戦�� -function websocketsend(agentData) { - websock.send(JSON.stringify(agentData)); -} - -//鍏抽棴 -function websocketclose() { - console.log("WebSocket宸插叧闂�"); -} -//WebSocket杩炴帴鎴愬姛 -function websocketOpen() { - console.log("WebSocket杩炴帴鎴愬姛"); -} - -function close() { - websock.close() -} - -export { sendSock, initWebSocket, close } \ No newline at end of file + // 鎵嬪姩鍏抽棴WebSocket + closeMyself() { + console.log('鎵ц鎵嬪姩鍏抽棴', new Date()); + this.status = 'close'; + this.ws.close(); + this.ws = null + } +} \ No newline at end of file -- Gitblit v1.9.1