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
相关推荐
Lost of 程序猿1 天前
ASP.NET Core Saga 分布式事务深度实战:备件采购跨服务长流程,如何保证“要么全成,要么全回“
分布式·后端·asp.net
XiYang-DING1 天前
地图城市缓存优化:ApplicationReadyEvent + Caffeine + Redis + Redisson 分布式锁
redis·分布式·缓存
2601_962175661 天前
RabbitMQ 的工作模式
分布式·rabbitmq
艾莉丝努力练剑1 天前
【AI大模型接入SDK】WebSocket & SSE 协议
网络·c++·websocket·网络协议·学习·面试
头茬韭菜1 天前
图解 Fluss(四):分布式协调 —— 选举、副本状态机与
分布式·fluss
jay神2 天前
【计算机毕业设计】基于Spring Boot的宠物领养管理系统
java·spring boot·后端·vue·毕业设计·宠物
要开心吖ZSH2 天前
腾讯 IM 前端对接小白教程:医患一对一聊天(含自定义报告卡片)
vue·健康医疗·即时通讯·im
2601_962218612 天前
万象生鲜系统全链路溯源一码查询技术实现食材来源可查
分布式·微服务·云原生·架构
Dreams_l2 天前
RabbitMQ介绍及其工作模式
分布式·rabbitmq