[Js进阶]WebSocket操作类封装

WebSocket操作类封装

参考Sonic平台

/**
 * WebSocket 封装
 * 主要功能:
 * 1. 自动重连;
 */
export default class Socket {
  // WebSocket 实例
  ws
  // 相关配置项
  options
  // 错误消息队列
  errorStack = []
  // 是否正在重连
  isReconnectLoading = false
  // 定时器超时重连 ID
  timeId = null

  constructor(options) {
    /**
     * options 支持传入参数
     * url websocket请求地址
     * node: 'player',
		 * mode: 'audio',
		 * debug: true,
		 * flushingTime: 0,
     * reconnectDelay: 3000,
     * onopen,
     * onmessage,
     * onerror,
     * onclose
     */
    this.options = {
      isErrorReconnect: true,
      ...options
    }
    this.init()
  }

  /**
   * 初始化 WebSocket
   */
  init() {
    if ('WebSocket' in window) {
      this.ws = new WebSocket(this.options.url);
      this.ws.binaryType = this.options.binaryType
      this.onOpen(this.options.onopen)
      this.onMessage(this.options.onmessage)
      this.onError(this.options.onerror)
      this.onClose(this.options.onclose)
    } else {
      console.error('该浏览器不支持WebSocket!');
    }
  }

  // 获取 WebSocket 实例
  get getWebSocket() {
    return this.ws
  }

  /**
   * 设置连接成功后的回调函数
   * @param {*} cb
   */
  onOpen(cb) {
    this.ws.onopen = () => {
      console.log('websocket >>>> onOpen 连接成功!')
      // 发送成功连接之前所发送失败的消息
      this.errorStack.forEach(message => {
        this.send(message)
      })
      cb && cb()
      this.errorStack = []
      this.isReconnectLoading = false
    }
  }

  /**
   * 设置接收 WebSocket 消息的回调函数
   * @param {*} cb 
   */
  onMessage(cb) {
    try {
      this.ws.onmessage = cb
    } catch (e) {
      console.error('error: ', e)
    }
  }

  /**
   * 设置连接失败后的回调函数
   * @param {*} cb 
   */
  onError(cb) {
    this.ws.onerror = (err) => {
      console.error(err, 'websocket >>>> onError 连接异常!')
      cb && cb(err)
      if (!this.options.isErrorReconnect) return
      this.onReconnection()
      this.isReconnectLoading = false
    }
  }

  /**
   * 设置连接关闭后的回调函数
   * @param {*} cb 
   */
  onClose(cb) {
    this.ws.onclose = () => {
      console.log('websocket >>>> onClose 关闭连接!')
      // 用户手动关闭的不重连
      if (this.isCustomClose) return
      cb && cb()
      this.onReconnection()
      this.isReconnectionLoading = false
    }
  }

  /**
   * 请求连接异常重连
   * @returns 
   */
  onReconnection() {
    // 重连请求延时
    const delay = this.options.reconnectDelay || 3000
    // 防止重复请求
    if (this.isReconnectionLoading) {
      console.log('websocket >>>> onReconnection 请勿重复请求连接!')
      return
    }
    console.log('websocket >>>> onReconnection 正在重连!')
    this.isReconnectionLoading = true
    clearTimeout(this.timeId)
    this.timeId = setTimeout(() => {
      this.init()
    }, delay)
  }

  /**
   * 手动发送请求
   * @param {*} message 
   * @returns 
   */
  handleSend(message) {
    // 连接失败时的处理
    if (this.ws.readyState !== WebSocket.OPEN) {
      console.error('websocket >>>> handleSend 请求发送失败!')
      this.errorStack.push(message)
      return
    }
    this.ws.send(message)
  }

  /**
   * 手动关闭连接
   */
  handleClose() {
    this.isCustomClose = true
    this.ws.close()
  }

  /**
   * 手动开启连接
   */
  handleStart() {
    this.isCustomClose = false
    this.onReconnection()
  }

  /**
   * 手动销毁连接实例
   */
  handleDestroy() {
    this.handleClose()
    this.ws = null
    this.errorStack = null
    console.log('websocket >>>> handleDestroy 实例已销毁!')
  }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值