当前位置: 首页 > article >正文

类组件化websocket的方法(心跳机制)

/**
 * WebSocket统一管理
 */
export class WebSocketClient {
    constructor(url) {
        if (!url) {
            throw new Error("WebSocket URL is required.");
        }
        this.url = url;
        this.websocket = null;
        this.listeners = {};
        this.heartbeatInterval = 30000; // 心跳检测间隔(毫秒)
        this.reconnectDelay = 10000; // 断线后重连间隔(毫秒)
        this.pingTimeout = null;
        this.reconnectTimeout = null;
        this.isManuallyClosed = false;
    }

    /** 初始化 WebSocket 连接 */

    connect() {
        this.websocket = new WebSocket(this.url);

        this.websocket.onopen = () => {
            // console.log("WebSocket connection opened.");
            // this.startHeartbeat(); // 开始心跳检测
            this.dispatch("open");
        };

        this.websocket.onmessage = (event) => {
            const data = JSON.parse(event.data ?? "{}");
            this.dispatch("message", data);
        };

        this.websocket.onclose = () => {
            // console.log("WebSocket connection closed.");
            this.dispatch("close");
            // this.stopHeartbeat(); // 停止心跳检测
            if (!this.isManuallyClosed) {
                this.reconnect(); // 自动重连
            }
        };

        this.websocket.onerror = (error) => {
            console.error("WebSocket error:", error);
            this.dispatch("error", error);
        };
    }

    // 关闭 WebSocket 连接
    close() {
        this.isManuallyClosed = true; // 手动关闭连接时,不进行重连
        if (this.websocket) {
            this.websocket.close();
        }
    }

    /**
     * 添加事件监听函数
     * @param { 'open' | 'message' | 'close' | 'error' } eventType
     * @param { (data) => void } callback
     */
    addListener(eventType, callback) {
        if (!this.listeners[eventType]) {
            this.listeners[eventType] = [];
        }
        this.listeners[eventType].push(callback);
    }

    /** 移除事件监听函数 */
    removeListener(eventType, callback) {
        if (!this.listeners[eventType]) return;

        this.listeners[eventType] = this.listeners[eventType].filter(
            (listener) => listener !== callback
        );
    }

    /** 派发事件 */
    dispatch(eventType, data) {
        if (!this.listeners[eventType]) return;

        this.listeners[eventType].forEach((listener) => listener(data));
    }

    // 心跳检测 (ping/pong)
    startHeartbeat() {
        if (this.pingTimeout) {
            clearTimeout(this.pingTimeout);
        }

        // 定时发送心跳
        this.pingTimeout = setTimeout(() => {
            if (this.websocket && this.websocket.readyState === WebSocket.OPEN) {
                this.websocket.send("ping"); // 发送心跳包
                console.log("Ping sent to server.");
            }

            // 继续心跳
            this.startHeartbeat();
        }, this.heartbeatInterval);
    }

    // 停止心跳检测
    stopHeartbeat() {
        if (this.pingTimeout) {
            clearTimeout(this.pingTimeout);
        }
    }

    // 自动重连
    reconnect() {
        console.log(`正在重连${this.url} ${this.reconnectDelay / 1000} 秒...`);
        this.reconnectTimeout = setTimeout(() => {
            this.connect();
        }, this.reconnectDelay);
    }

    send(data) {
        this.websocket.send(data);
    }
}


http://www.kler.cn/a/299178.html

相关文章:

  • 漫谈设计模式 [8]:装饰器模式
  • C语言:刷题日志(3)
  • 【QT】文件读写,文件对话框
  • 2024国赛数学建模预测算法-BP神经网络模型的预测精度分析(MATLAB 实现)
  • 寄存器间接寻址与寄存器直接寻址
  • 【32单片机篇】项目:智能台灯
  • 百易云资产管理运营系统 house.save.php SQL注入漏洞
  • 【C++ 面试 - 新特性】每日 3 题(九)
  • Ionic 头部和底部
  • 若依漏洞综合利用工具
  • 用于完善智能电表设计的 FPGA 到 ASIC 研究
  • 【C++二分查找 滑动窗口】2831. 找出最长等值子数组
  • hutool 集合相关交集、差集
  • A表和B表公共元素产生链表C
  • 各类软件在Linux上的安装
  • 多人开发小程序设置体验版的痛点
  • Vue知识点笔记(持续更新)
  • 使用 `readResolve` 防止序列化破坏单例模式
  • 【JVM】JVM栈帧中的动态链接 与 Java的面向对象特性--多态
  • 系统工程建模MBSE