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>

三、提前说明

明天开始调试链接业务

相关推荐
麦麦大数据1 天前
F024 RNN+Vue+Flask电影推荐可视化系统 python flask mysql 深度学习 echarts
python·rnn·深度学习·vue·echarts·电影推荐
SirLancelot11 天前
MongoDB-基本介绍(一)基本概念、特点、适用场景、技术选型
java·数据库·分布式·后端·mongodb·软件工程·软件构建
koping_wu1 天前
【Kafka】架构原理、消息丢失、重复消费、顺序消费、事务消息
分布式·架构·kafka
爱吃芒果的蘑菇1 天前
C++之WebSocket初体验
网络·c++·websocket·网络协议
柳贯一(逆流河版)1 天前
Redis 分布式锁实战:解决马拉松报名并发冲突与 Lua 原子性优化
redis·分布式·lua
Komorebi゛1 天前
【Vue3】使用websocket实现前后端实时更新数据
前端·websocket
ajax_beijing2 天前
hadoop的三副本数据冗余策略
大数据·hadoop·分布式
失散132 天前
分布式专题——46 ElasticSearch高级查询语法Query DSL实战
java·分布式·elasticsearch·架构
没有bug.的程序员2 天前
分布式链路追踪:微服务可观测性的核心支柱
java·分布式·微服务·架构·wpf
C.R.xing2 天前
Pyspark分布式访问NebulaGraph图数据库
数据库·分布式·python·pyspark·nebulagraph