WebSocket封装(TypeScript、单例模式、自动重连、事件监听、Vue中使用)

6 篇文章 1 订阅
3 篇文章 0 订阅
文章介绍了如何在Vue3应用中使用SocketService类进行WebSocket连接的管理,包括连接状态监控、自动重连选项设置、事件监听和解除监听等功能。
摘要由CSDN通过智能技术生成
export type AutoReconnectOptions = boolean | {
  maxRetries?: number
  retryInterval?: number
  onMaxRetriesReached?: Function
}

export enum ConnectionStatus {
  Disconnected = 'DISCONNECTED',
  Connected = 'CONNECTED',
  Error = 'ERROR'
}

class SocketService {
  private static instance: SocketService | null = null
  private ws: WebSocket | null = null
  private listeners: Record<string, Function[]> = {}
  private autoReconnect: AutoReconnectOptions = true
  private retries: number = 0
  private connectionStatus: ConnectionStatus = ConnectionStatus.Disconnected

  private constructor() {
    this.connect()
  }

  public static getInstance(): SocketService {
    if (!SocketService.instance) {
      SocketService.instance = new SocketService()
    }
    return SocketService.instance
  }

  public setAutoReconnectOptions(options: AutoReconnectOptions) {
    this.autoReconnect = options
  }

  public connect() {
    this.ws = new WebSocket('ws://localhost:3000/ws')
    this.ws.onopen = () => {
      this.connectionStatus = ConnectionStatus.Connected
      this.emit('connected', null)
    }
    this.ws.onerror = () => {
      this.connectionStatus = ConnectionStatus.Error
      this.emit('error', null)
    }
    this.ws.onclose = () => {
      this.connectionStatus = ConnectionStatus.Disconnected
      this.emit('disconnected', null)
      if (this.shouldReconnect()) {
        setTimeout(() => this.connect(), this.getRetryInterval())
      }
    }
    this.ws.onmessage = (event) => {
      this.emit('message', event.data)
    }
  }

  private shouldReconnect(): boolean {
    if (typeof this.autoReconnect === 'boolean') {
      return this.autoReconnect
    } else if (this.autoReconnect) {
      const { maxRetries } = this.autoReconnect

      if (maxRetries !== undefined) {
        if (this.retries < maxRetries) {
          this.retries++
          return true
        } else if (this.retries >= maxRetries) {
          this.autoReconnect.onMaxRetriesReached && this.autoReconnect.onMaxRetriesReached()
          return false
        }
      }
    }
    return false
  }

  private getRetryInterval(): number {
    if (typeof this.autoReconnect === 'boolean') {
      return 1000
    } else if (this.autoReconnect && this.autoReconnect.retryInterval) {
      return this.autoReconnect.retryInterval
    }
    return 1000
  }

  public send(data: any) {
    if (this.ws && this.ws.readyState === WebSocket.OPEN) {
      this.ws.send(JSON.stringify(data))
    } else {
      console.error('WebSocket 连接未打开')
    }
  }

  public close() {
    if (this.ws) {
      this.ws.close()
      SocketService.instance = null
    }
  }

  private emit(event: string, data: any) {
    if (!this.listeners[event]) {
      return
    }
    this.listeners[event].forEach((listener) => listener(data))
  }

  public on(event: string, listener: Function) {
    if (!this.listeners[event]) {
      this.listeners[event] = []
    }
    this.listeners[event].push(listener)
    if (event === 'connected' && this.connectionStatus === ConnectionStatus.Connected) {
      listener()
    }
  }

  public off(event: string, listener: Function) {
    if (!this.listeners[event]) {
      return
    }
    this.listeners[event] = this.listeners[event].filter((l) => l !== listener)
  }

  public getConnectionStatus(): ConnectionStatus {
    return this.connectionStatus
  }

  public watchConnectionStatus(callback: (status: ConnectionStatus) => void) {
    this.on('connected', () => callback(ConnectionStatus.Connected))
    this.on('disconnected', () => callback(ConnectionStatus.Disconnected))
    this.on('error', () => callback(ConnectionStatus.Error))
  }
}

export default SocketService

在 Vue3 中使用:

<script setup lang="ts">
import { onBeforeUnmount, ref } from 'vue'
import SocketService from '@/lib/SocketService'

const socketService = SocketService.getInstance()

const connectionStatus = ref(socketService.getConnectionStatus())
socketService.watchConnectionStatus((status) => {
  connectionStatus.value = status
})

socketService.setAutoReconnectOptions({
  maxRetries: 4,
  retryInterval: 2000,
  onMaxRetriesReached: () => {
    console.log('max retries reached')
  }
})

const onConnected = () => {
  console.log('connected')
}
socketService.on('connected', onConnected)

const onDisconnected = () => {
  console.log('disconnected')
}
socketService.on('disconnected', onDisconnected)

const onMessage = (data: any) => {
  console.log('message', data)
}
socketService.on('message', onMessage)

onBeforeUnmount(() => {
  socketService.off('connected', onConnected)
  socketService.off('disconnected', onDisconnected)
  socketService.off('message', onMessage)
})
</script>

<template>
  <div>
    <div>Connection status: {{ connectionStatus }}</div>
    <button @click="socketService.connect">Connect</button>
    <button @click="socketService.close">Close</button>
    <button @click="socketService.send('Hello')">Send</button>
  </div>
</template>

可以在不同的 Vue 组件/页面中通过 SocketService.getInstance() 获取同一个 WebSocket 连接,监听数据接收以及发送消息。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值