Browse Source

删除websocket

fangzhen 7 months ago
parent
commit
353eeecfa7
3 changed files with 0 additions and 152 deletions
  1. 0 29
      ruoyi-ui/src/main.js
  2. 0 5
      ruoyi-ui/src/store/modules/user.js
  3. 0 118
      ruoyi-ui/src/utils/websocket.js

+ 0 - 29
ruoyi-ui/src/main.js

@@ -37,8 +37,6 @@ import DictTag from '@/components/DictTag'
 import VueMeta from 'vue-meta'
 // 字典数据组件
 import DictData from '@/components/DictData'
-// webSocket工具
-import webSocket from "./utils/webSocket"
 
 // 全局方法挂载
 Vue.prototype.getDicts = getDicts
@@ -50,7 +48,6 @@ Vue.prototype.selectDictLabel = selectDictLabel
 Vue.prototype.selectDictLabels = selectDictLabels
 Vue.prototype.download = download
 Vue.prototype.handleTree = handleTree
-Vue.prototype.$websocket = webSocket
 
 // 全局组件挂载
 Vue.component('DictTag', DictTag)
@@ -81,32 +78,6 @@ Vue.use(Element, {
 
 Vue.config.productionTip = false
 
-let newVue = new Vue({
-  el: '#app',
-  created() {
-    //监听用户窗口是否关闭
-    window.addEventListener('beforeunload', this.closeSocket);
-  },
-  destroyed() {
-    window.removeEventListener('beforeunload', this.closeSocket);
-  },
-  methods: {
-    onBeforeUnload(event) {
-      // 在这里编写你想要执行的代码
-      // 例如:发送数据到服务器或者显示警告信息
-      // 设置event.returnValue以显示浏览器默认的警告信息
-      event.returnValue = '您可能有未保存的更改!';
-    },
-    closeSocket() {
-      //关闭websocket连接
-      this.$websocket.close();
-    }
-  },
-  router,
-  store,
-  render: h => h(App)
-})
-
 new Vue({
   el: '#app',
   router,

+ 0 - 5
ruoyi-ui/src/store/modules/user.js

@@ -1,6 +1,5 @@
 import { login, logout, getInfo } from '@/api/login'
 import { getToken, setToken, removeToken } from '@/utils/auth'
-import newVue from "@/main";
 
 const user = {
   state: {
@@ -66,8 +65,6 @@ const user = {
           commit('SET_ID', user.userId)
           commit('SET_NAME', user.userName)
           commit('SET_AVATAR', avatar)
-          //TODO 获取用户信息时检查socket连接状态并进行连接
-          newVue.$websocket.initWebSocket();
           resolve(res)
         }).catch(error => {
           reject(error)
@@ -82,8 +79,6 @@ const user = {
           commit('SET_TOKEN', '')
           commit('SET_ROLES', [])
           commit('SET_PERMISSIONS', [])
-          //TODO 用户退出登录后关闭连接
-          newVue.$websocket.close();
           removeToken()
           resolve()
         }).catch(error => {

+ 0 - 118
ruoyi-ui/src/utils/websocket.js

@@ -1,118 +0,0 @@
-import { Notification } from "element-ui";
-import { getToken } from "./auth";
-import store from '../store'
-
-var socket = null;//实例对象
-var lockReconnect = false; //是否真正建立连接
-var timeout = 20 * 1000; //20秒一次心跳
-var timeoutObj = null; //心跳倒计时
-var serverTimeoutObj = null; //服务心跳倒计时
-var timeoutnum = null; //断开 重连倒计时
-
-const initWebSocket = async () => {
-  if ("WebSocket" in window) {
-    if (!store.state.user.id) {
-      console.log("未登录!websocket工具获取不到userId")
-    }else {
-      const wsUrl = process.env.VUE_APP_SOCKET_SERVER + store.state.user.id;
-      socket = new WebSocket(wsUrl);
-      socket.onerror = webSocketOnError;
-      socket.onmessage = webSocketOnMessage;
-      socket.onclose = closeWebsocket;
-      socket.onopen = openWebsocket;
-    }
-  } else {
-    Notification.error({
-      title: "错误",
-      message: "您的浏览器不支持websocket,请更换Chrome或者Firefox",
-    });
-  }
-}
-
-//建立连接
-const openWebsocket = (e) => {
-  start();
-}
-
-const start = ()=> {
-  //开启心跳
-  timeoutObj && clearTimeout(timeoutObj);
-  serverTimeoutObj && clearTimeout(serverTimeoutObj);
-  timeoutObj = setTimeout(function() {
-    //这里发送一个心跳,后端收到后,返回一个心跳消息
-    if (socket.readyState == 1) {
-      //如果连接正常
-      // socket.send("heartbeat");
-    } else {
-      //否则重连
-      reconnect();
-    }
-    serverTimeoutObj = setTimeout(function() {
-      //超时关闭
-      socket.close();
-    }, timeout);
-  }, timeout);
-}
-
-//重新连接
-const reconnect =() => {
-  if (lockReconnect) {
-    return;
-  }
-  lockReconnect = true;
-  //没连接上会一直重连,设置延迟避免请求过多
-  timeoutnum && clearTimeout(timeoutnum);
-  timeoutnum = setTimeout(function() {
-    //新连接
-    initWebSocket();
-    lockReconnect = false;
-  }, 1000);
-}
-
-//重置心跳
-const reset =() => {
-  //清除时间
-  clearTimeout(timeoutObj);
-  clearTimeout(serverTimeoutObj);
-  //重启心跳
-  start();
-}
-
-const sendWebsocket = (message) =>{
-  socket.send(message);
-}
-
-const webSocketOnError = (e) => {
-  initWebSocket();
-  reconnect();
-
-}
-
-//服务器返回的数据
-const webSocketOnMessage = (e)=> {
-  //判断是否登录
-  if (getToken()) {
-    //window自定义事件
-    window.dispatchEvent(
-      new CustomEvent("onmessageWS", {
-        detail: {
-          data: JSON.parse(e?.data)
-        },
-      })
-    );
-  }
-  // socket.onmessage(e)
-  reset();
-}
-
-const closeWebsocket=(e) => {
-  reconnect();
-}
-
-//断开连接
-const close =() => {
-//WebSocket对象也有发送和关闭的两个方法,只需要在自定义方法中分别调用send()和close()即可实现。
-  socket.close();
-}
-//具体问题具体分析,把需要用到的方法暴露出去
-export default { initWebSocket, sendWebsocket, webSocketOnMessage, close };