最佳websocket封装

embedded/2024/10/20 8:21:00/

封装了weboskect,完美支持了断网重连、自动心跳的功能,且完全兼容原生写法,无任何学习负担,开开箱即用!

import { EventDispatcher } from './dispatcher';export class WebSocketClient extends EventDispatcher {// #socket链接url = '';// #socket实例socket = null;// #重连次数reconnectAttempts = 0;// #最大重连数maxReconnectAttempts = 5;// #重连间隔reconnectInterval = 10000; // 10 seconds// #发送心跳数据间隔heartbeatInterval = 1000 * 30;// #计时器idheartbeatTimer = undefined;// #彻底终止wsstopWs = false;// *构造函数constructor(url) {super();this.url = url;}// >生命周期钩子onopen(callBack) {this.addEventListener('open', callBack);}onmessage(callBack) {this.addEventListener('message', callBack);}onclose(callBack) {this.addEventListener('close', callBack);}onerror(callBack) {this.addEventListener('error', callBack);}// >消息发送send(message) {if (this.socket && this.socket.readyState === WebSocket.OPEN) {this.socket.send(message);} else {console.error('[WebSocket] 未连接');}}// !初始化连接connect() {if (this.reconnectAttempts === 0) {this.log('WebSocket', `初始化连接中...          ${this.url}`);}if (this.socket && this.socket.readyState === WebSocket.OPEN) {return;}this.socket = new WebSocket(this.url);// !websocket连接成功this.socket.onopen = event => {this.stopWs = false;// 重置重连尝试成功连接this.reconnectAttempts = 0;// 在连接成功时停止当前的心跳检测并重新启动this.startHeartbeat();this.log('WebSocket', `连接成功,等待服务端数据推送[onopen]...     ${this.url}`);this.dispatchEvent('open', event);};this.socket.onmessage = event => {this.dispatchEvent('message', event);this.startHeartbeat();};this.socket.onclose = event => {if (this.reconnectAttempts === 0) {this.log('WebSocket', `连接断开[onclose]...    ${this.url}`);}if (!this.stopWs) {this.handleReconnect();}this.dispatchEvent('close', event);};this.socket.onerror = event => {if (this.reconnectAttempts === 0) {this.log('WebSocket', `连接异常[onerror]...    ${this.url}`);}this.closeHeartbeat();this.dispatchEvent('error', event);};}// > 断网重连逻辑handleReconnect() {if (this.reconnectAttempts < this.maxReconnectAttempts) {this.reconnectAttempts++;this.log('WebSocket', `尝试重连... (${this.reconnectAttempts}/${this.maxReconnectAttempts})       ${this.url}`);setTimeout(() => {this.connect();}, this.reconnectInterval);} else {this.closeHeartbeat();this.log('WebSocket', `最大重连失败,终止重连: ${this.url}`);}}// >关闭连接close() {if (this.socket) {this.stopWs = true;this.socket.close();this.socket = null;this.removeEventListener('open');this.removeEventListener('message');this.removeEventListener('close');this.removeEventListener('error');}this.closeHeartbeat();}// >开始心跳检测 -> 定时发送心跳消息startHeartbeat() {if (this.stopWs) return;if (this.heartbeatTimer) {this.closeHeartbeat();}this.heartbeatTimer = setInterval(() => {if (this.socket) {this.socket.send(JSON.stringify({ type: 'heartBeat', data: {} }));this.log('WebSocket', '送心跳数据...');} else {console.error('[WebSocket] 未连接');}}, this.heartbeatInterval);}// >关闭心跳closeHeartbeat() {clearInterval(this.heartbeatTimer);this.heartbeatTimer = undefined;}
}
class Log {static console = true;log(title, text) {if (!Log.console) return;if (import.meta.env.MODE === 'production') return;const color = '#ff4d4f';console.log(`%c ${title} %c ${text} %c`,`background:${color};border:1px solid ${color}; padding: 1px; border-radius: 2px 0 0 2px; color: #fff;`,`border:1px solid ${color}; padding: 1px; border-radius: 0 2px 2px 0; color: ${color};`,'background:transparent');}closeConsole() {Log.console = false;}
}
export class EventDispatcher extends Log {listeners = {};addEventListener(type, listener) {if (!this.listeners[type]) {this.listeners[type] = [];}if (this.listeners[type].indexOf(listener) === -1) {this.listeners[type].push(listener);}}removeEventListener(type) {this.listeners[type] = [];}dispatchEvent(type, data) {const listenerArray = this.listeners[type] || [];if (listenerArray.length === 0) return;listenerArray.forEach(listener => {listener.call(this, data);});}
}

http://www.ppmy.cn/embedded/53786.html

相关文章

二叉树 | Java | LeetCode 235 701 450 做题总结,BST特性、 调整二叉树结构(增+删)

235. 二叉搜索树的最近公共祖先 思路&#xff1a;要利用二叉搜索数的性质。当前遍历节点 cur 的数值大于p q时&#xff0c;说明 p q 的父节点在 cur 的左子树。当前遍历节点 cur 的数值小于p q时&#xff0c;说明 p q 的父节点在 cur 的右子树。当前遍历节点 cur 的数值在 p q…

【Linux详解】冯诺依曼架构 | 操作系统设计 | 斯坦福经典项目Pintos

目录 一. 冯诺依曼体系结构 (Von Neumann Architecture) 注意事项 存储器的意义&#xff1a;缓冲 数据流动示例 二. 操作系统 (Operating System) 操作系统的概念 操作系统的定位与目的 操作系统的管理 系统调用和库函数 操作系统的管理&#xff1a; sum 三. 系统调…

小公司全栈是归宿吗?

在软件开发领域&#xff0c;特别是在小公司或初创公司中&#xff0c;全栈开发者的角色确实相对普遍和重要。然而&#xff0c;说“全栈是归宿”可能过于绝对&#xff0c;因为每个开发者的职业路径和兴趣点都是不同的。 以下是关于全栈开发在小公司的一些考虑&#xff1a; 需求…

大模型性能优化KV Cache

原理 KV Cache的本质就是避免重复计算&#xff0c;把需要重复计算的结果进行缓存&#xff0c;生成式模型的新的token的产生需要用到之前的所有token的 K , V K,V K,V&#xff0c;在计算注意力的时候是当前的 Q Q Q和所有的 K , V K,V K,V来进行计算&#xff0c;所以是缓存 K ,…

PHP框架之Laravel框架

Laravel框架详解 Laravel&#xff0c;作为一款广受欢迎的PHP Web开发框架&#xff0c;以其优雅、简洁的语法和强大的功能特性&#xff0c;赢得了全球众多开发者的青睐。下面&#xff0c;我们将从Laravel的特点、应用案例以及具体的框架使用等方面进行详细解析。 一、Laravel框…

【Text2SQL 论文】MCS-SQL:利用多样 prompts + 多项选择来做 Text2SQL

论文&#xff1a;MCS-SQL: Leveraging Multiple Prompts and Multiple-Choice Selection For Text-to-SQL Generation ⭐⭐⭐ arXiv:2405.07467 一、论文速读 已有研究指出&#xff0c;在使用 LLM 使用 ICL 时&#xff0c;ICL 的 few-shot exemplars 的内容、呈现顺序都会敏感…

深度分析:Apache Hadoop及其在大数据处理中的应用

引言 在大数据处理领域&#xff0c;Apache Hadoop因其强大的分布式计算能力和存储能力&#xff0c;已成为数据密集型应用的核心技术。自2006年开源以来&#xff0c;Hadoop已经发展成为一个庞大的生态系统&#xff0c;支持各种数据处理任务。本文将深入分析Hadoop的核心特点&am…

使用Kafka框架发送和接收消息(Java示例)

Kafka是一个开源的分布式流处理平台&#xff0c;以其在大数据和实时处理领域的广泛应用而闻名。以下是Kafka的关键特性以及它在消息传输方面的优势&#xff1a; 高吞吐量与低延迟&#xff1a;Kafka能够每秒处理数百万条消息&#xff0c;具有极低的延迟&#xff0c;这使得它非常…