websocket自动重连封装

websocket自动重连封装

前端代码封装

javascript 复制代码
import { ref, onUnmounted } from 'vue';

interface WebSocketOptions {
  url: string;
  protocols?: string | string[];
  reconnectTimeout?: number;
}

class WebSocketService {
  private ws: WebSocket | null = null;
  private callbacks: { [key: string]: Function[] } = {};
  private reconnectTimeoutMs: number = 5000; // 默认5秒重连间隔

  constructor(private options: WebSocketOptions) { }

  public open(): void {
    this.ws = new WebSocket(this.options.url, this.options.protocols)
    this.ws.addEventListener('open', this.handleOpen);
    this.ws.addEventListener('message', this.handleMessage);
    this.ws.addEventListener('error', this.handleError);
    this.ws.addEventListener('close', this.handleClose);
  }

  public close(isActiveClose = false): void {
    if (this.ws) {
      this.ws.close();
      if (!isActiveClose) {
        setTimeout(() => this.reconnect(), this.reconnectTimeoutMs);
      }
    }
  }

  public reconnect(): void {
    this.open();
  }

  public on(event: 'message', callback: (data: any) => void): void;
  public on(event: 'open' | 'error' | 'close', callback: () => void): void;
  public on(event: string, callback: (...args: any[]) => void): void {
    if (!this.callbacks[event]) {
      this.callbacks[event] = [];
    }
    this.callbacks[event].push(callback);
  }

  private handleOpen = (): void => {
    console.log('WebSocket连接已建立');
    if (this.callbacks.open) {
      this.callbacks.open.forEach((cb) => cb());
    }
  };

  private handleMessage = (event: MessageEvent): void => {
    console.log(event,"event");
    const data = JSON.parse(event.data);
    console.log('WebSocket接收到消息:', data);
    if (this.callbacks.message) {
      this.callbacks.message.forEach((cb) => cb(data));
    }
  };

  private handleError = (error: Event): void => {
    console.error('WebSocket错误:', error);
    if (this.callbacks.error) {
      this.callbacks.error.forEach((cb) => cb(error));
    }
  };

  private handleClose = (): void => {
    console.log('WebSocket连接已关闭');
    if (this.callbacks.close) {
      this.callbacks.close.forEach((cb) => cb());
      if (!this.options.reconnectTimeout) {
        this.reconnect();
      }
    }
  };

  public send(data: any): void {
    if (this.ws && this.ws.readyState === WebSocket.OPEN) {
      this.ws.send(JSON.stringify(data));
    } else {
      console.warn('尝试发送消息时WebSocket未连接');
    }
  }
}

export default function useWebSocket(options: WebSocketOptions) {
  const wsService = new WebSocketService(options);

  onUnmounted(() => {
    wsService.close(true);
  });

  return {
    open: wsService.open.bind(wsService),
    close: wsService.close.bind(wsService),
    reconnect: wsService.reconnect.bind(wsService),
    on: wsService.on.bind(wsService),
    send: wsService.send.bind(wsService)
  };
}

组件中使用

JavaScript 复制代码
<script setup lang="ts">
import { onMounted } from 'vue'
import useWebSocket from "@/utils/websocket";
const os = useWebSocket({ url: 'http://localhost:3000' })

const onSend = () => {
  os.send({ text: '你好' })
}

onMounted(async () => {
  os.open()
  os.on('message', (event) => {
    console.log(event, "event");
  })
});
</script>

后端代码封装

javascript 复制代码
const express = require('express');
const bodyParser = require('body-parser');
const http = require('http');
const WebSocket = require('ws');
const cors = require('cors');
const app = express();
const server = http.createServer(app);

const corsOption = {
  origin: 'http://localhost:8088',
  methods: ['GET', 'POST', 'PUT', 'DELETE'],
  allowedHeaders: ['Content-Type', 'Authorization'],
}

app.use(cors(corsOption))

// 增加请求体大小限制
app.use(bodyParser.json({ limit: '100mb' }));  // 允许最多10MB的JSON数据
app.use(bodyParser.urlencoded({ limit: '100mb', extended: true }));

// 初始化 WebSocket 服务器实例
const wss = new WebSocket.Server({ server });

// 监听 WebSocket 连接事件
wss.on('connection', (ws) => {
  console.log('客户端已连接');
  // 监听消息
  ws.on('message', (message) => {
    console.log('收到消息:', JSON.parse(message));
    const postMsg = {
      msg: "你好"
    }
    // 回复客户端
    ws.send(JSON.stringify(postMsg));
  });

  // 监听关闭事件
  ws.on('close', () => {
    console.log('客户端已断开连接');
  });
});


// 设置 Express 路由
app.get('/', (req, res) => {
  res.send('WebSocket Server Running');
});

// 启动服务器
server.listen(3000, () => {
  console.log('服务器在 http://localhost:3000 运行');
});
相关推荐
倾国倾城的反派修仙者3 分钟前
鸿蒙开发——使用弹窗授权保存媒体库资源
开发语言·前端·华为·harmonyos
鱼跃鹰飞17 分钟前
面试题:知道WebSocket协议吗?
网络·websocket·网络协议
泰勒疯狂展开18 分钟前
Vue3研学-组件的生命周期
开发语言·前端·vue
Charlie_lll21 分钟前
学习Three.js–基于GeoJSON绘制2D矢量地图
前端·three.js
计算机学姐29 分钟前
基于SpringBoot的自习室座位预定系统【预约选座+日期时间段+协同过滤推荐算法+数据可视化统计】
java·vue.js·spring boot·后端·spring·信息可视化·tomcat
小二·39 分钟前
Python Web 开发进阶实战:AI 原生安全防护 —— 在 Flask + Suricata 中构建智能网络威胁狩猎平台
前端·人工智能·python
葡萄城技术团队1 小时前
SpreadJS V19.0 新特性解密:设计器容器行列合计,让报表数据汇总更灵活
前端
晚霞的不甘1 小时前
Flutter for OpenHarmony:从零到一:构建购物APP的骨架与精美UI
前端·javascript·flutter·ui·前端框架·鸿蒙
小二·1 小时前
Python Web 开发进阶实战:AI 原生硬件接口 —— 在 Flask + MicroPython 中构建边缘智能设备控制平台
前端·人工智能·python
ElasticPDF-新国产PDF编辑器1 小时前
基于 PDF.js 的 PDF 文字编辑解决方案,纯前端 SDK,跨平台、框架无关、Web 原生
前端·javascript·pdf