¶Ô±ÈÐÂÎļþ |
| | |
| | | /** |
| | | * @module initWebSocket åå§å |
| | | * @module websocketonopen è¿æ¥æå |
| | | * @module websocketonerror è¿æ¥å¤±è´¥ |
| | | * @module websocketclose æå¼è¿æ¥ |
| | | * @module resetHeart éç½®å¿è·³ |
| | | * @module sendSocketHeart å¿è·³åé |
| | | * @module reconnect éè¿ |
| | | * @module sendMsg åéæ°æ® |
| | | * @module websocketonmessage æ¥æ¶æ°æ® |
| | | * @module test æµè¯æ¶å°æ¶æ¯ä¼ é |
| | | * @description socket éä¿¡ |
| | | * @param {any} url socketå°å |
| | | * @param {any} websocket websocket å®ä¾ |
| | | * @param {any} heartTime å¿è·³å®æ¶å¨å®ä¾ |
| | | * @param {number} socketHeart å¿è·³æ¬¡æ° |
| | | * @param {number} HeartTimeOut å¿è·³è¶
æ¶æ¶é´ |
| | | * @param {number} socketError éè¯¯æ¬¡æ° |
| | | */ |
| | | |
| | | import { getToken } from '@/utils/auth'; |
| | | import useNoticeStore from '@/store/modules/notice'; |
| | | import { ElNotification } from "element-plus"; |
| | | |
| | | const { addNotice } = useNoticeStore(); |
| | | |
| | | let socketUrl: any = ''; // socketå°å |
| | | let websocket: any = null; // websocket å®ä¾ |
| | | let heartTime: any = null; // å¿è·³å®æ¶å¨å®ä¾ |
| | | let socketHeart = 0 as number; // å¿è·³æ¬¡æ° |
| | | const HeartTimeOut = 10000; // å¿è·³è¶
æ¶æ¶é´ 10000 = 10s |
| | | let socketError = 0 as number; // éè¯¯æ¬¡æ° |
| | | |
| | | // åå§åsocket |
| | | export const initWebSocket = (url: any) => { |
| | | if (!import.meta.env.VITE_APP_WEBSOCKET) { |
| | | return; |
| | | } |
| | | socketUrl = url; |
| | | // åå§å websocket |
| | | websocket = new WebSocket(url + '?Authorization=Bearer ' + getToken() + '&clientid=' + import.meta.env.VITE_APP_CLIENT_ID); |
| | | websocketonopen(); |
| | | websocketonmessage(); |
| | | websocketonerror(); |
| | | websocketclose(); |
| | | sendSocketHeart(); |
| | | return websocket; |
| | | }; |
| | | |
| | | // socket è¿æ¥æå |
| | | export const websocketonopen = () => { |
| | | websocket.onopen = function () { |
| | | console.log('è¿æ¥ websocket æå'); |
| | | resetHeart(); |
| | | }; |
| | | }; |
| | | |
| | | // socket è¿æ¥å¤±è´¥ |
| | | export const websocketonerror = () => { |
| | | websocket.onerror = function (e: any) { |
| | | console.log('è¿æ¥ websocket 失败', e); |
| | | }; |
| | | }; |
| | | |
| | | // socket æå¼é¾æ¥ |
| | | export const websocketclose = () => { |
| | | websocket.onclose = function (e: any) { |
| | | console.log('æå¼è¿æ¥', e); |
| | | }; |
| | | }; |
| | | |
| | | // socket éç½®å¿è·³ |
| | | export const resetHeart = () => { |
| | | socketHeart = 0; |
| | | socketError = 0; |
| | | clearInterval(heartTime); |
| | | sendSocketHeart(); |
| | | }; |
| | | |
| | | // socketå¿è·³åé |
| | | export const sendSocketHeart = () => { |
| | | heartTime = setInterval(() => { |
| | | // å¦æè¿æ¥æ£å¸¸ååéå¿è·³ |
| | | if (websocket.readyState == 1) { |
| | | // if (socketHeart <= 30) { |
| | | websocket.send( |
| | | JSON.stringify({ |
| | | type: 'ping' |
| | | }) |
| | | ); |
| | | socketHeart = socketHeart + 1; |
| | | } else { |
| | | // éè¿ |
| | | reconnect(); |
| | | } |
| | | }, HeartTimeOut); |
| | | }; |
| | | |
| | | // socketéè¿ |
| | | export const reconnect = () => { |
| | | if (socketError <= 2) { |
| | | clearInterval(heartTime); |
| | | initWebSocket(socketUrl); |
| | | socketError = socketError + 1; |
| | | // eslint-disable-next-line prettier/prettier |
| | | console.log('socketéè¿', socketError); |
| | | } else { |
| | | // eslint-disable-next-line prettier/prettier |
| | | console.log('éè¯æ¬¡æ°å·²ç¨å®'); |
| | | clearInterval(heartTime); |
| | | } |
| | | }; |
| | | |
| | | // socket åéæ°æ® |
| | | export const sendMsg = (data: any) => { |
| | | websocket.send(data); |
| | | }; |
| | | |
| | | // socket æ¥æ¶æ°æ® |
| | | export const websocketonmessage = () => { |
| | | websocket.onmessage = function (e: any) { |
| | | if (e.data.indexOf('heartbeat') > 0) { |
| | | resetHeart(); |
| | | } |
| | | if (e.data.indexOf('ping') > 0) { |
| | | return; |
| | | } |
| | | addNotice({ |
| | | message: e.data, |
| | | read: false, |
| | | time: new Date().toLocaleString() |
| | | }); |
| | | ElNotification({ |
| | | title: 'æ¶æ¯', |
| | | message: e.data, |
| | | type: 'success', |
| | | duration: 3000 |
| | | }) |
| | | return e.data; |
| | | }; |
| | | }; |