IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> websocket 封装及使用 -> 正文阅读

[网络协议]websocket 封装及使用

1.websocket.js? ?封装好了,直接在项目中引入使用即可

class jtWebSocket{
  //构造函数
  constructor() {
    this.webSocket = null; //webSocket对象
    this.url = null; //webSocket连接的url
    this.lastHeartBeat = 0; // 上一次心跳时间
    this.connectTimer = null; // 重连定时器
    this.isPauseConnet = null; // 是否要暂停连接
  }
  initWebSocket (url) { // 初始化weosocket
    this.url = url
    this.webSocket = new WebSocket(this.url)
    this.lastHeartBeat = new Date().getTime()
    if (this.webSocket) {
      this.webSocket.onopen = this.websocketonopen
      this.webSocket.onerror = this.websocketonerror
      this.webSocket.onclose = this.websocketclose
    }
  }
  websocketonopen () { // 连接建立之后执行send方法发送数据
    clearInterval(this.connectTimer)
    this.isPauseConnet = true
    this.connectTimer = null
    this.connectTimer = setInterval(this.checkConnect, 5000)
  }
   // 连接建立失败重连
  websocketonerror (callback) {
    this.isLoadImg = false
    if(this.webSocket&&this.webSocket.readyState){
      if (this.webSocket.readyState === 3 || this.webSocket.readyState === 4) {
        if (this.isPauseConnet === false) {
          this.webSocket = null
        }
      }
    }
    callback()
  }
  // 断开了连接
  websocketclose () {
    if(this.webSocket&&this.webSocket.readyState){
      if (this.webSocket.readyState === 3 || this.webSocket.readyState === 4) {
        if (this.isPauseConnet === false) {
          this.webSocket = null
        }
      }
    }
  }
  destroywebsocket () {
    // 清除定时器 关闭websocket
    this.isPauseConnet = false
    clearInterval(this.connectTimer)
    this.connectTimer = null
    if (this.webSocket) {
      this.webSocket.close()
    }
  }
  // 断线重连
  checkConnect () {
    if (this.connetNum > 3) {
      return
    }
    if ((new Date().getTime() - this.lastHeartBeat) > 8000) {
      if (this.webSocket) {
        if (this.webSocket.readyState === 3 || this.webSocket.readyState === 4) {
          if (this.isPauseConnet === true) {
            this.initWebSocket()
            this.connetNum++
          }
        }
      } else {
        if (this.isPauseConnet === true) {
          this.initWebSocket()
          this.connetNum++
        }
      }
    } else {
      let obj = {
        type: 'connect'
      }
      this.websocketsend(obj)
    }
  }
  // 监听消息
  websocketonmessage (callback) {
    this.webSocket.onmessage = e =>{
      let data = JSON.parse(e.data)
      this.lastHeartBeat = new Date().getTime()
      if(callback) callback(data)
    }
  }
  // 发送数据
  websocketsend (Data) { // 数据发送
    if (this.webSocket !== null && this.webSocket.readyState === 1){
      this.webSocket.send(JSON.stringify(Data))
    }
  }
}
export default jtWebSocket

2. 在vue中的使用,目前发的是在vue中的使用,在别的框架里面使用的话需要稍微改下使用方式即可

(1)引入js

import jtWebSocket from "../../service/websocket";

(2)使用

data () {
  return {
    websocket:null
  }
}
methods: {
    // 初始化websocket相关
    initWebSocket(){
       this.websocket = new jtWebSocket()
      this.websocket.initWebSocket('ws://127.0.0.1/xxx')
      this.websocket.websocketonmessage(data=>{
        console.log(data) // 及时通信数据
      })
      // 连接出错监听
      this.websocket.websocketonerror(()=>{
        //监听到连接出错后的处理
      })
    },
    // ws 发送消息
    handlePostMsg(data){
      this.websocket.websocketsend(data)
    },
    // 断开websocket连接
    destroyWs () {
      this.websocket.destroywebsocket()
    },
}

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2021-07-10 14:49:19  更:2021-07-10 14:50:05 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/26 13:38:53-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码