From 9e89ab5bae8e52ca8d7bb17940cb7860ca573968 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期二, 14 十一月 2023 11:11:07 +0800
Subject: [PATCH] !57 发布 vue 版本 5.1.1 与 cloud 版本2.1.1 Merge pull request !57 from 疯狂的狮子Li/dev

---
 src/utils/websocket.ts |  141 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 141 insertions(+), 0 deletions(-)

diff --git a/src/utils/websocket.ts b/src/utils/websocket.ts
new file mode 100644
index 0000000..5ba0243
--- /dev/null
+++ b/src/utils/websocket.ts
@@ -0,0 +1,141 @@
+/**
+ * @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; // 閿欒娆℃暟
+
+// 鍒濆鍖杝ocket
+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(() => {
+    // 濡傛灉杩炴帴姝e父鍒欏彂閫佸績璺�
+    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;
+  };
+};

--
Gitblit v1.9.3