js 封装websocket (包括心跳检测,断线重连等)

效果图

在这里插入图片描述

js 模块使用


export const newWebSocket = {
  websocket: null,
  heartbeat_timer: null, // 用于心跳检测
  active_close: false, // 是否主动关闭
  init: (params) => {
    newWebSocket.websocket = new WebSocket(params.url)
    newWebSocket.websocket.onopen = (data) => {
      newWebSocket.heartbeat()
      if (params.onopen) {
        params.onopen('webSocket已连接', data)
      }
    }
    newWebSocket.websocket.onmessage = (data) => {
      if (params.onmessage) {
        params.onmessage(data.data)
      }
    }
    newWebSocket.websocket.onclose = () => {
      clearInterval(newWebSocket.heartbeat_timer)
      if (newWebSocket.active_close) {
        if (params.onclose) {
          params.onclose('连接已手动关闭')
        }
      } else {
        newWebSocket.reconnect(params)
        if (params.onclose) {
          params.onclose('连接已关闭, 正在重连')
        }
      }
    }
    newWebSocket.websocket.onerror = () => {
      clearInterval(newWebSocket.heartbeat_timer)
      if (params.onerror) {
        params.message('连接发生错误!等待五秒后重连')
        setTimeout(() => {
          newWebSocket.reconnect(params)
        },5000)
      }
    }
  },
  heartbeat: () => {
    console.log('执行心跳')
    if (newWebSocket.heartbeat_timer) {
      clearInterval(newWebSocket.heartbeat_timer)
    }
    newWebSocket.heartbeat_timer = setInterval(function (){
      newWebSocket.websocket.send('heartbeat')
    },10000)
  },
  reconnect: (params) => {
    console.log('执行重连')
    newWebSocket.active_close = false
    if (newWebSocket.websocket) {
      newWebSocket.websocket.close()
    }
    newWebSocket.init(params)
  },
  close: () => {
    console.log('手动关闭,无需重连')
    clearInterval(newWebSocket.heartbeat_timer)
    newWebSocket.active_close = true
    newWebSocket.websocket.close()
  }
}


引入封装好的newWebSocket

import { newWebSocket } from "../../plugins/bwWebSocket";


使用方法

translateWs () {
      newWebSocket.init({
        url: wssUrl, // 自己的ws 地址
        onopen: (msg, data) => {
          console.log(msg, data)
        },
        onmessage: (data) => {
          console.log(data)
          this.handleRefresh('swim')
        },
        onclose: (data) => {
          console.log(data)
        }
      })
    },

页面离开前手动关闭

  destroyed() {
    newWebSocket.close()
  },
`
  mounted() {
    this.translateWs()
  },``

需要多个websocket的项目可以改成构造函数方式,不需要则无所谓

export function NewWebSocket () {
  this.websocket = null
  this.heartbeat_timer = null
  this.active_close = null
  this.init = (params) => {
    this.websocket = new WebSocket(params.url)
    this.websocket.onopen = (data) => {
      this.heartbeat()
      if (params.onopen) {
        params.onopen('webSocket已连接', data)
      }
    }
    this.websocket.onmessage = (data) => {
      if (params.onmessage) {
        params.onmessage(data.data)
      }
    }
    this.websocket.onclose = () => {
      clearInterval(this.heartbeat_timer)
      if (this.active_close) {
        if (params.onclose) {
          params.onclose('连接已手动关闭')
        }
      } else {
        this.reconnect(params)
        if (params.onclose) {
          params.onclose('连接已关闭, 正在重连')
        }
      }
    }
    this.websocket.onerror = () => {
      clearInterval(this.heartbeat_timer)
      if (params.onerror) {
        params.message('连接发生错误!等待五秒后重连')
        setTimeout(() => {
          this.reconnect(params)
        },5000)
      }
    }
  }
  this.heartbeat = () => {
    console.log('执行心跳')
    if (this.heartbeat_timer) {
      clearInterval(this.heartbeat_timer)
    }
    this.heartbeat_timer = setInterval(() =>{
      this.websocket.send('heartbeat')
    },10000)
  }
  this.reconnect = (params) => {
    console.log('执行重连')
    this.active_close = false
    if (this.websocket) {
      this.websocket.close()
    }
    this.init(params)
  }
  this.close = () => {
    console.log('手动关闭,无需重连')
    clearInterval(this.heartbeat_timer)
    this.active_close = true
    this.websocket.close()
  }
}

使用方式

      const wssUrl = `ws:/******`
      this.ws = new NewWebSocket()
      this.ws.init({
        url: wssUrl, // 自己的ws 地址
        onopen: (msg, data) => {
          console.log(msg, data)
        },
        onmessage: (data) => {
          console.log(data)
        },
        onclose: (data) => {
          console.log(data)
        }

销毁

this.ws.close()
  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值