workman服务端开发模式-应用开发-vue-element-admin封装websocket

一、用于保存WebSocket实例对象

复制代码
export const WebSocketHandle = undefined

二、外部根据具体登录地址实例化WebSocket,然后回传保存WebSocket

复制代码
export const WebsocketINI = function (websocketinstance) {
  this.WebSocketHandle = websocketinstance
  this.WebSocketHandle.onmessage = OnMessage
}

三、为实例化的WebSocket绑定消息接收事件:同时用于回调外部各个vue页面绑定的消息事件

复制代码
const OnMessage = function(msg) {
  // 1、消息打印
  // console.log('收到消息:', msg)

  // 2、如果外部回调函数未绑定 结束操作
  if (!WebSocket.WebSocketOnMsgEvent_CallBack) {
    console.log(WebSocket.WebSocketOnMsgEvent_CallBack)
    return
  }

  // 3、调用外部函数
  WebSocket.WebSocketOnMsgEvent_CallBack(msg)
}

四、全局存放外部页面绑定onmessage消息回调函数:注意使用的是var

复制代码
export const WebSocketOnMsgEvent_CallBack = undefined

五、外部通过此绑定方法 来传入的onmessage消息回调函数

复制代码
export const WebSocketBandMsgReceivedEvent = function(receiveevent) {
  WebSocket.WebSocketOnMsgEvent_CallBack = receiveevent
}

六、封装一个直接发送消息的方法:

复制代码
export const Send = function(msg) {
  if (!this.WebSocketHandle || this.WebSocketHandle.readyState !== 1) {
    // 未创建连接 或者连接断开 无法发送消息
    return
  }
  this.WebSocketHandle.send(msg)// 发送消息
}

七、导出配置

复制代码
const WebSocket = {
  WebSocketHandle,
  WebsocketINI,
  WebSocketBandMsgReceivedEvent,
  Send,
  WebSocketOnMsgEvent_CallBack
}

八、全局绑定WebSocket

复制代码
Vue.prototype.$WebSocket = WebSocket

九、总结

复制代码
import Vue from 'vue'

// 1、用于保存WebSocket实例对象
export const WebSocketHandle = undefined

// 2、外部根据具体登录地址实例化WebSocket,然后回传保存WebSocket
export const WebsocketINI = function (websocketinstance) {
  this.WebSocketHandle = websocketinstance
  this.WebSocketHandle.onmessage = OnMessage
}
// 3、为实例化的WebSocket绑定消息接收事件:同时用于回调外部各个vue页面绑定的消息事件
// 主要使用WebSocket.WebSocketOnMsgEvent_CallBack才能访问  this.WebSocketOnMsgEvent_CallBack 无法访问很诡异
const OnMessage = function(msg) {
  // 1、消息打印
  // console.log('收到消息:', msg)

  // 2、如果外部回调函数未绑定 结束操作
  if (!WebSocket.WebSocketOnMsgEvent_CallBack) {
    console.log(WebSocket.WebSocketOnMsgEvent_CallBack)
    return
  }

  // 3、调用外部函数
  WebSocket.WebSocketOnMsgEvent_CallBack(msg)
}

// 4、全局存放外部页面绑定onmessage消息回调函数:注意使用的是var
export const WebSocketOnMsgEvent_CallBack = undefined

// 5、外部通过此绑定方法 来传入的onmessage消息回调函数
export const WebSocketBandMsgReceivedEvent = function(receiveevent) {
  WebSocket.WebSocketOnMsgEvent_CallBack = receiveevent
}

// 6、封装一个直接发送消息的方法:
export const Send = function(msg) {
  if (!this.WebSocketHandle || this.WebSocketHandle.readyState !== 1) {
    // 未创建连接 或者连接断开 无法发送消息
    return
  }
  this.WebSocketHandle.send(msg)// 发送消息
}

// 7、导出配置
const WebSocket = {
  WebSocketHandle,
  WebsocketINI,
  WebSocketBandMsgReceivedEvent,
  Send,
  WebSocketOnMsgEvent_CallBack
}

// 8、全局绑定WebSocket
Vue.prototype.$WebSocket = WebSocket
相关推荐
zzz_236813 分钟前
【RabbitMQ】面试系列 · 第一期:基础认知与选型实战
分布式·面试·rabbitmq
SilentSamsara25 分钟前
Python 微服务全链路:gRPC + 链路追踪 + 服务网格接入
开发语言·分布式·python·微服务·架构
zzz_236842 分钟前
【Redis】分布式锁完整演进
数据库·redis·分布式
AI人工智能+电脑小能手1 小时前
【大白话说Java面试题 第99题】【Mysql篇】第29题:如何选择合适的分布式主键方案?
java·数据库·分布式·mysql·面试
happyprince1 小时前
11-Hugging Face Transformers 分布式与并行系统深度分析
分布式·c#·wpf
不知名的老吴1 小时前
在Spinklock中分布式锁的概念
分布式
zhangfeng11331 小时前
天数智芯天垓 100 加密大模型分布式部署安全方案
人工智能·分布式·安全·transformer·gpu算力·芯片
giaz14n9X2 小时前
Redis 分布式锁进阶第五十一篇
数据库·redis·分布式
AIFQuant3 小时前
量化私募回测系统:高质量股票/外汇历史数据 API 选型与接入
python·websocket·金融·ai量化
2603_954708314 小时前
微电网协调控制系统柜的应用场景有哪些?
分布式·安全·架构·能源·需求分析