guoshilong
2023-09-11 11433d0f47eae3403e883fc26d72269b930235f2
src/websocket/index.ts
@@ -1,5 +1,7 @@
import { message } from 'ant-design-vue'
import ReconnectingWebSocket from 'reconnecting-websocket'
import { ELocalStorageKey } from '../types'
interface WebSocketOptions {
  data: any
@@ -35,14 +37,14 @@
    if (!this._url) {
      return
    }
    const token: string = localStorage.getItem(ELocalStorageKey.Token) || '' as string
    // 会自动重连,无需处理重连逻辑
    console.log(token, 'token')
    this._socket = new ReconnectingWebSocket(this._url, [], {
      maxReconnectionDelay: 20000, // 断开后最大的重连时间: 20s,每多一次重连,会增加 1.3 倍,5 * 1.3 * 1.3 * 1.3...
      minReconnectionDelay: 5000, // 断开后最短的重连时间: 5s
      maxRetries: 5
      maxRetries: 5,
    })
    this._hasInit = true
    this._socket.addEventListener('open', this._onOpen.bind(this))