workman服务端开发模式-应用开发-vue-element-admin挂载websocket

一、项目根目录main.js添加全局引入

复制代码
import '@/utils/websocket'

二、在根目录app.vue 中初始化WebSocket连接

复制代码
<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
  import store from './store'
export default {
  name: 'App',
  created() {
    // 因为我的页面有缓存机制,用户下次有可能直接打开某个登录后才能访问的页面 比如F5刷新了某个页面 需要重连
    // 又比如后端服务器因为什么原因突然中断了一下 也需要重新连接WebSocket
    // 每3秒检测一次websocket连接状态 未连接 则尝试连接 尽量保证网站启动的时候 WebSocket都能正常长连接
    setInterval(this.WebSocket_StatusCheck, 2000)
  },
  methods: {
    // 1、WebSocket连接状态检测:
    WebSocket_StatusCheck() {
      //如果登录了情况下,验证是否已经绑定
      if(store.getters.token != undefined || store.getters.token != null){
        if (!this.$WebSocket.WebSocketHandle || this.$WebSocket.WebSocketHandle.readyState !== 1) {
          this.WebSocketINI()
        }else{
          this.$WebSocket.WebSocketHandle.send(JSON.stringify({type: 'ping'}))
        }
      }
    },

    // 2、WebSocket初始化:
    async WebSocketINI() {
      // 1、浏览器是否支持WebSocket检测
      if (!('WebSocket' in window)) {
        return
      }

      // 2、从后台提取WebScoket服务器连接地址:根据自己业务接口获取 或者直接跳过 下面直接写死
      /*const tmpResource = await this.$Api.Resource.Get('OtherSets_WebSocket_Address').then(res => {
          return res
      })*/
      const tmpWebsocketSrverAddress = 'ws://你的地址'//可以直接赋值如:ws://127.0.0.1:1234
      // 3、创建Websocket连接
      const tmpWebsocket = new WebSocket(tmpWebsocketSrverAddress)
      /*const protocols = ['base64url.bearer.authorization.sockjs.nodecode.ly']; // 自定义header
      const tmpWebsocket = new SockJS(tmpWebsocketSrverAddress)*/
      // 4、全局保存WebSocket操作句柄:main.js 全局引用
      this.$WebSocket.WebsocketINI(tmpWebsocket)

      // 5、WebSocket连接成功提示
      tmpWebsocket.onopen = function(ev){

      }
      tmpWebsocket.onmessage = function(ev){
        const datas = JSON.parse(ev.data)
        if(datas.type == 'ping'){
          if(datas.client_id != undefined){
            store.dispatch('user/bindInfo',{client_id:datas.client_id}).then(res => {
              if(res == 2){

              }else{
                this.WebSocketINI()
              }
            })
          }
        }
        if(datas.type == 'close'){
          if(datas.eve == 'repeat'){
            store.dispatch('user/repeatToken')
          }else if(datas.eve == 'expire'){
            store.dispatch('user/expireToken')
          }
        }
      }
      //6、连接失败提示
      tmpWebsocket.onclose = function(ev) {
        console.log(ev)
        store.dispatch('user/resetToken')
      }
    }
  }
}
</script>

三、提前说明

明天开始调试链接业务

相关推荐
_xaboy2 小时前
开源表单设计器 FcDesigner 保存表单教程:toJson parseJson 回显
低代码·开源·vue·表单·fcdesigner
Databuff3 小时前
使用Pinpoint作分布式链路跟踪系统
运维·分布式·运维开发·开源软件
传感器与混合集成电路3 小时前
分布式光纤测温DTS系统:ZDTS-P2000与X1000参数对比及找漏监测应用
分布式·数据分析
一条泥憨鱼3 小时前
【从0开始学习计算机网络】| WebSocket-握手、全双工和心跳
websocket·计算机网络·网络安全·https·dns
传感器与混合集成电路3 小时前
分布式光纤声波DAS测井系统:0.1Hz低频检测、0.1m采样间隔与6066m实井对比
分布式
MetaLite3 小时前
SpringBoot整合Caffeine-集群本地缓存如何保证分布式一致性
spring boot·分布式·缓存
钛态12 小时前
Vite 中的 CSS 工程化:从 CSS Modules 到 UnoCSS 的渐进式迁移
前端·vue·react·web
GitLqr21 小时前
从 TCP 底层看实时通信:为什么 SSE 往往比 WebSocket 更香?
websocket·http·openai
肠畔码农1 天前
深入分布式事务内核:从 2PC/XA 到 Seata AT 模式的架构演进与权衡
分布式·架构
ai小陈1 天前
PyTorch多GPU分布式训练实战:从单卡脚本迁移到DDP
服务器·人工智能·pytorch·分布式·深度学习·ai·gpu算力