JavaScript系列(62)--实时通信系统实现详解
JavaScript实时通信系统实现详解 🔄
今天,让我们深入探讨JavaScript的实时通信系统实现。实时通信是现代Web应用中不可或缺的一部分,它能够提供即时的数据交互和更好的用户体验。
WebSocket通信基础 🌟
💡 小知识:WebSocket是HTML5开始提供的一种在单个TCP连接上进行全双工通讯的协议。相比HTTP,它能够提供持久连接和双向通信能力。
// 1. WebSocket连接管理器
class WebSocketManager {constructor(url, options = {}) {this.url = url;this.options = {reconnectAttempts: 5,reconnectDelay: 1000,heartbeatInterval: 30000,...options};this.connection = null;this.reconnectCount = 0;this.listeners = new Map();this.heartbeatTimer = null;}// 建立连接connect() {try {this.connection = new WebSocket(this.url);this.setupEventListeners();this.startHeartbeat();} catch (error) {this.handleConnectionError(error);}}// 设置事件监听器setupEventListeners() {this.connection.onopen = () => {this.reconnectCount = 0;this.emit('connected');};this.connection.onclose = () => {this.handleDisconnect();};this.connection.onerror = (error) => {this.handleConnectionError(error);};this.connection.onmessage = (event) => {this.handleMessage(event.data);};}// 启动心跳检测startHeartbeat() {this.heartbeatTimer = setInterval(() => {if (this.connection.readyState === WebSocket.OPEN) {this.send('heartbeat', { timestamp: Date.now() });}}, this.options.heartbeatInterval);}// 处理断开连接handleDisconnect() {this.stopHeartbeat();this.emit('disconnected');if (this.reconnectCount < this.options.reconnectAttempts) {setTimeout(() => {this.reconnectCount++;this.connect();}, this.options.reconnectDelay * this.reconnectCount);} else {this.emit('maxReconnectAttemptsReached');}}// 发送消息send(type, data) {if (this.connection.readyState !== WebSocket.OPEN) {throw new Error('Connection is not open');}const message = JSON.stringify({ type, data });this.connection.send(message);}// 停止心跳检测stopHeartbeat() {if (this.heartbeatTimer) {clearInterval(this.heartbeatTimer);this.heartbeatTimer = null;}}
}// 2. 消息处理器
class MessageHandler {constructor() {this.handlers = new Map();}// 注册消息处理器register(type, handler) {if (!this.handlers.has(type)) {this.handlers.set(type, new Set());}this.handlers.get(type).add(handler);}// 处理消息async handle(message) {const { type, data } = JSON.parse(message);const handlers = this.handlers.get(type);if (handlers) {const promises = Array.from(handlers).map(handler => handler(data));await Promise.all(promises);}}
}// 3. 重连管理器
class ReconnectionManager {constructor(options = {}) {this.options = {maxAttempts: 5,baseDelay: 1000,maxDelay: 30000,...options};this.attempts = 0;this.currentDelay = this.options.baseDelay;}// 计算下一次重连延迟getNextDelay() {const delay = Math.min(this.currentDelay * Math.pow(2, this.attempts),this.options.maxDelay);this.attempts++;return delay;}// 重置重连状态reset() {this.attempts = 0;this.currentDelay = this.options.baseDelay;}// 检查是否可以继续重连canReconnect() {return this.attempts < this.options.maxAttempts;}
}
消息队列系统 📨
// 1. 消息队列
class MessageQueue {constructor() {this.queue = [];this.processing = false;this.maxRetries = 3;}// 添加消息enqueue(message) {this.queue.push({message,retries: 0,timestamp: Date.now()});this.processQueue();}// 处理队列async processQueue() {if (this.processing || this.queue.length === 0) return;this.processing = true;while (this.queue.length > 0) {const item = this.queue[0];try {await this.processMessage(item.message);this.queue.shift();} catch (error) {if (item.retries < this.maxRetries) {item.retries++;// 移到队列末尾this.queue.push(this.queue.shift());} else {// 放入死信队列this.moveToDeadLetter(item);this.queue.shift();}}}this.processing = false;}// 移动到死信队列moveToDeadLetter(item) {// 实现死信队列逻辑}
}// 2. 优先级队列
class PriorityMessageQueue {constructor() {this.queues = new Map();this.priorities = ['high', 'medium', 'low'];}// 添加消息enqueue(message, priority = 'medium') {if (!this.queues.has(priority)) {this.queues.set(priority, []);}this.queues.get(priority).push({message,timestamp: Date.now()});}// 获取下一个消息dequeue() {for (const priority of this.priorities) {const queue = this.queues.get(priority);if (queue && queue.length > 0) {return queue.shift();}}return null;}
}// 3. 消息持久化管理器
class MessagePersistenceManager {constructor() {this.storage = new Map();this.initStorage();}// 初始化存储async initStorage() {try {const stored = localStorage.getItem('message_queue');if (stored) {const data = JSON.parse(stored);this.storage = new Map(Object.entries(data));}} catch (error) {console.error('Failed to initialize storage:', error);}}// 保存消息async persistMessage(id, message) {this.storage.set(id, {message,timestamp: Date.now()});await this.saveToStorage();}// 保存到存储async saveToStorage() {try {const data = Object.fromEntries(this.storage);localStorage.setItem('message_queue', JSON.stringify(data));} catch (error) {console.error('Failed to save to storage:', error);}}
}
实时数据同步 🔄
// 1. 实时数据同步器
class RealtimeDataSync {constructor(options = {}) {this.options = {syncInterval: 1000,batchSize: 100,...options};this.changes = new Map();this.syncTimer = null;}// 记录变更recordChange(key, value) {this.changes.set(key, {value,timestamp: Date.now()});this.scheduleSyncIfNeeded();}// 调度同步scheduleSyncIfNeeded() {if (!this.syncTimer && this.changes.size > 0) {this.syncTimer = setTimeout(() => {this.performSync();}, this.options.syncInterval);}}// 执行同步async performSync() {const batch = this.prepareSyncBatch();if (batch.size > 0) {try {await this.sendChanges(batch);this.clearSyncedChanges(batch);} catch (error) {this.handleSyncError(error);}}this.syncTimer = null;this.scheduleSyncIfNeeded();}// 准备同步批次prepareSyncBatch() {const batch = new Map();let count = 0;for (const [key, value] of this.changes) {if (count >= this.options.batchSize) break;batch.set(key, value);count++;}return batch;}
}// 2. 冲突解决器
class ConflictResolver {constructor() {this.strategies = new Map();this.setupDefaultStrategies();}// 设置默认策略setupDefaultStrategies() {this.strategies.set('lastWriteWins', (local, remote) => {return local.timestamp > remote.timestamp ? local : remote;});this.strategies.set('merge', (local, remote) => {return {...local,...remote,timestamp: Math.max(local.timestamp, remote.timestamp)};});}// 解决冲突resolve(local, remote, strategy = 'lastWriteWins') {const resolver = this.strategies.get(strategy);if (!resolver) {throw new Error(`Unknown strategy: ${strategy}`);}return resolver(local, remote);}
}// 3. 版本控制管理器
class VersionManager {constructor() {this.versions = new Map();this.history = new Map();}// 更新版本updateVersion(key, value) {const currentVersion = this.versions.get(key) || 0;const newVersion = currentVersion + 1;this.versions.set(key, newVersion);this.recordHistory(key, value, newVersion);return newVersion;}// 记录历史recordHistory(key, value, version) {if (!this.history.has(key)) {this.history.set(key, new Map());}const keyHistory = this.history.get(key);keyHistory.set(version, {value,timestamp: Date.now()});}// 获取特定版本getVersion(key, version) {const keyHistory = this.history.get(key);if (!keyHistory) return null;return keyHistory.get(version);}
}
性能优化策略 ⚡
// 1. 消息压缩器
class MessageCompressor {constructor() {this.compressionThreshold = 1024; // 1KB}// 压缩消息async compress(message) {if (typeof message !== 'string') {message = JSON.stringify(message);}if (message.length < this.compressionThreshold) {return message;}const msgBuffer = new TextEncoder().encode(message);const compressed = await gzip(msgBuffer);return compressed;}// 解压消息async decompress(data) {if (!(data instanceof Uint8Array)) {return data;}const decompressed = await ungzip(data);return new TextDecoder().decode(decompressed);}
}// 2. 批处理优化器
class BatchProcessor {constructor(options = {}) {this.options = {maxBatchSize: 100,maxWaitTime: 1000,...options};this.batch = [];this.timer = null;}// 添加项目到批处理add(item) {this.batch.push(item);if (this.batch.length >= this.options.maxBatchSize) {this.flush();} else if (!this.timer) {this.timer = setTimeout(() => this.flush(), this.options.maxWaitTime);}}// 刷新批处理async flush() {if (this.timer) {clearTimeout(this.timer);this.timer = null;}if (this.batch.length === 0) return;const items = [...this.batch];this.batch = [];await this.processBatch(items);}
}// 3. 连接池管理器
class ConnectionPool {constructor(options = {}) {this.options = {maxConnections: 5,idleTimeout: 30000,...options};this.connections = new Set();this.idle = new Set();}// 获取连接async getConnection() {let connection;if (this.idle.size > 0) {connection = this.idle.values().next().value;this.idle.delete(connection);} else if (this.connections.size < this.options.maxConnections) {connection = await this.createConnection();this.connections.add(connection);} else {throw new Error('Connection pool exhausted');}return connection;}// 释放连接releaseConnection(connection) {if (this.connections.has(connection)) {this.idle.add(connection);setTimeout(() => {if (this.idle.has(connection)) {this.closeConnection(connection);}}, this.options.idleTimeout);}}
}
安全性考虑 🔒
// 1. 消息加密器
class MessageEncryptor {constructor() {this.keyPair = null;this.initializeKeyPair();}// 初始化密钥对async initializeKeyPair() {this.keyPair = await window.crypto.subtle.generateKey({name: 'RSA-OAEP',modulusLength: 2048,publicExponent: new Uint8Array([1, 0, 1]),hash: 'SHA-256'},true,['encrypt', 'decrypt']);}// 加密消息async encrypt(message) {const encoded = new TextEncoder().encode(typeof message === 'string' ? message : JSON.stringify(message));return window.crypto.subtle.encrypt({name: 'RSA-OAEP'},this.keyPair.publicKey,encoded);}// 解密消息async decrypt(encrypted) {const decrypted = await window.crypto.subtle.decrypt({name: 'RSA-OAEP'},this.keyPair.privateKey,encrypted);return new TextDecoder().decode(decrypted);}
}// 2. 认证管理器
class AuthenticationManager {constructor() {this.tokens = new Map();}// 验证令牌async validateToken(token) {if (!token) return false;const tokenInfo = this.tokens.get(token);if (!tokenInfo) return false;if (tokenInfo.expiresAt < Date.now()) {this.tokens.delete(token);return false;}return true;}// 生成新令牌async generateToken(userId) {const token = await this.createSecureToken();this.tokens.set(token, {userId,expiresAt: Date.now() + 24 * 60 * 60 * 1000 // 24小时});return token;}
}// 3. 速率限制器
class RateLimiter {constructor(options = {}) {this.options = {windowMs: 60000, // 1分钟maxRequests: 100,...options};this.requests = new Map();}// 检查请求是否允许async checkLimit(clientId) {this.removeOldRequests(clientId);const requests = this.requests.get(clientId) || [];if (requests.length >= this.options.maxRequests) {return false;}requests.push(Date.now());this.requests.set(clientId, requests);return true;}// 移除过期请求记录removeOldRequests(clientId) {const now = Date.now();const windowStart = now - this.options.windowMs;const requests = this.requests.get(clientId) || [];const validRequests = requests.filter(time => time > windowStart);if (validRequests.length < requests.length) {this.requests.set(clientId, validRequests);}}
}
最佳实践建议 💡
- 连接管理模式
// 1. 连接状态管理器
class ConnectionStateManager {constructor() {this.state = 'disconnected';this.listeners = new Set();}// 更新状态setState(newState) {const oldState = this.state;this.state = newState;this.notifyListeners(oldState, newState);}// 添加状态监听器addListener(listener) {this.listeners.add(listener);}// 通知监听器notifyListeners(oldState, newState) {for (const listener of this.listeners) {listener(oldState, newState);}}
}// 2. 重试策略
class RetryStrategy {constructor(options = {}) {this.options = {initialDelay: 1000,maxDelay: 30000,factor: 2,maxAttempts: 5,...options};}// 计算延迟时间getDelay(attempt) {const delay = this.options.initialDelay * Math.pow(this.options.factor, attempt);return Math.min(delay, this.options.maxDelay);}// 检查是否应该重试shouldRetry(attempt, error) {if (attempt >= this.options.maxAttempts) {return false;}// 根据错误类型决定是否重试return this.isRetryableError(error);}
}// 3. 日志记录器
class CommunicationLogger {constructor() {this.logs = [];this.maxLogs = 1000;}// 记录日志log(type, data) {const logEntry = {type,data,timestamp: Date.now()};this.logs.push(logEntry);if (this.logs.length > this.maxLogs) {this.logs.shift();}this.persistLogs();}// 持久化日志persistLogs() {try {localStorage.setItem('communication_logs', JSON.stringify(this.logs));} catch (error) {console.error('Failed to persist logs:', error);}}
}
结语 📝
实时通信系统是现代Web应用中的重要组成部分。通过本文,我们学习了:
- WebSocket通信的基础实现
- 消息队列系统的设计
- 实时数据同步机制
- 性能优化策略
- 安全性考虑和最佳实践
💡 学习建议:在实现实时通信系统时,要特别注意连接的可靠性和消息的可靠传递。同时,要根据实际需求选择合适的同步策略,平衡实时性和系统负载。
如果你觉得这篇文章有帮助,欢迎点赞收藏,也期待在评论区看到你的想法和建议!👇
终身学习,共同成长。
咱们下一期见
💻
相关文章:
JavaScript系列(62)--实时通信系统实现详解
JavaScript实时通信系统实现详解 🔄 今天,让我们深入探讨JavaScript的实时通信系统实现。实时通信是现代Web应用中不可或缺的一部分,它能够提供即时的数据交互和更好的用户体验。 WebSocket通信基础 🌟 💡 小知识&am…...
网络工程师 (20)计算机网络的概念
一、定义 计算机网络是指将地理位置不同、具有独立功能的多台计算机及其外部设备,通过通信线路及通信设备连接起来,在网络操作系统、网络管理软件及网络通信协议的管理和协调下,实现信息传递和资源共享的计算机通信系统。 二、组成 资源子网&…...
Unity UI Default Shader分析
文章目录 UI默认材质和Default ShaderShader的属性定义Mask组件支持RectMask2D组件支持其他支持使用Unity UGUI时经常有自定义shader的需求,虽然我们可以直接按照shader lab的规范写出shader,使用也没问题,但如果能让自定义shader符合UI shader的规范,支持Mask,Rect2DMask…...
IEEE 802.3/802.2 | LLC / SNAP
注:本文为 “IEEE 802.3/802.2 | LLC / SNAP” 相关文章合辑。 未整理去重。 第三篇部分内容出自第二篇。 802.2 协议 haoay321 2010-01-28 20:52:02 LLC 协议 LLC(Logic Link Control,逻辑链路控制)是 IEEE 802.2 协议中规定…...
【Linux】24.进程间通信(3)
文章目录 3.6 systemv共享内存3.6.1 共享内存函数3.6.3 一个简单的共享内存代码实现3.6.4 一个复杂的共享内存代码实现3.6.4 key和shmid的主要区别: 3.7 systemv消息队列(了解)3.8 systemv信号量(了解)进程互斥四个问题理解信号量…...
【自然语言处理】TextRank 算法提取关键词(Python实现)
文章目录 前言PageRank 实现TextRank 简单版源码实现jieba工具包实现TextRank 前言 TextRank 算法是一种基于图的排序算法,主要用于文本处理中的关键词提取和文本摘要。它基于图中节点之间的关系来评估节点的重要性,类似于 Google 的 PageRank 算法。Tex…...
Java-128陷阱、抽象类和接口的区别、为什么 hashCode()需要和equals()一起重写、封装继承多态
128陷阱 Integer a 100; Integer b 100; System.out.println(ab); //true Integer c 1000; Integer d 1000; System.out.println(cd);//false int e 1000; System.out.println(ce);//true 分析以上代码运行的结果 源码: Integer a128; 编译器执行的是&…...
使用 Python 编程语言来实现机器学习小项目教程案例
以下是一个简单的机器学习小项目教程案例,使用 Python 编程语言和 Scikit-learn 库来实现一个分类任务。我们将使用经典的鸢尾花(Iris)数据集来训练一个分类器,预测鸢尾花的种类。 项目目标 使用机器学习算法对鸢尾花数据集进行分类,预测鸢尾花的类别(Setosa、Versicolor…...
如何评价镜头的好坏?光学镜头的一种评价标准
1光学传递函数MTF MTF是什么? 光学传递函数(optical transfer function)是指以空间频率为变量,表征成像过程中调制度和横向相移的相对变化的函数。光学传递函数是光学系统对空间频谱的滤波变换。一个非相干照明的光学成像系统&a…...
openGauss 3.0 数据库在线实训课程1:学习数据库状态查看
openGauss数据库状态查看 前提 我正在参加21天养成好习惯| 第二届openGauss每日一练活动 课程详见:openGauss 3.0.0数据库在线实训课程 学习目标 学习从操作系统层面和使用openGauss工具查看数据库的状态、版本和数据文件目录。 课程作业 gs_ctl是openGauss提…...
Stable Diffusion的入门介绍和使用教程
Stable Diffusion是一个文本到图像的潜在扩散模型,由CompVis、StabilityAI和LAION的研究人员和工程师创建。它使用来自LAION-5B数据库子集的512x512图像进行训练。使用这个模型,可以生成包括人脸在内的任何图像,因为有开源的预训练模型&#…...
docker安装es及分词器ik
系统是macos,docker是docker-desktop 拉取镜像 docker pull bitnami/elasticsearch 启动docker镜像 docker create -e "discovery.typesingle-node" \ --name elasticsearch1 -p 9200:9200 -p 9300:9300 \ bitnami/elasticsearch:8.17.1 测试是否好…...
记一次框架利用接管学工系统
视频教程在我主页简介或专栏里 链接:观看更多 Springboot actuator (1)某学院学工管理系统存在Springboot actuator未授权,泄露了很多接口地址,其他接口就不过多介绍了,这里具体讲述这次利用到的httptrace和jolokia两…...
低代码提升交付效率的公式计算
低(无)代码平台(后统称“低代码”)能够提升数字化应用建设、交付效率,已经成为IT从业人员的共识。目前,大部分CIO/CDO都能清晰定位和认知低代码的特点和作用。但仍然有人认为,使用了低代码工具软…...
深入解析:如何利用 Python 爬虫获取商品 SKU 详细信息
在电商领域,SKU(Stock Keeping Unit,库存单位)详细信息是电商运营的核心数据之一。它不仅包含了商品的规格、价格、库存等关键信息,还直接影响到库存管理、价格策略和市场分析等多个方面。本文将详细介绍如何利用 Pyth…...
java后端开发面试常问
面试常问问题 1 spring相关 (1)Transactional失效的场景 <1> Transactional注解默认只会回滚运行时异常(RuntimeException),如果方法中抛出了其他异常,则事务不会回滚(数据库数据仍然插…...
第六期:开放银行突围战 - API经济下的跨域经营合规框架
一、监管沙盒中的API兵法 1.1 开放银行接口的军备等级 人行《商业银行应用程序接口管理规范》(2025修订版): 安全分级: L1(查询类):日均调用量≤10万次 (如余额查询) L2(交易类):必须双因素认证 (如转账) L3(决策类):需人工智能审计跟踪 (如授信评估) 实战接口设计…...
全程Kali linux---CTFshow misc入门(25-37)
第二十五题: 提示:flag在图片下面。 直接检查CRC,检测到错误,就直接暴力破解。 暴力破解CRC的python代码。 import binascii import struct def brute_force_ihdr_crc(filename): # 读取文件二进制数据 with open(filen…...
Axure大屏可视化动态交互设计:解锁数据魅力,引领决策新风尚
可视化组件/模板预览:https://8dge09.axshare.com 一、大屏可视化技术概览 在数据驱动决策的时代,大屏可视化技术凭借直观、动态的展示方式,已成为众多行业提升管理效率和优化决策过程的关键工具。它能够将复杂的数据转化为易于理解的图形和…...
《AI “造脸术”:生成对抗网络打造超真实虚拟人脸》
在科技飞速发展的当下,人工智能的浪潮席卷而来,其中生成对抗网络(GANs)技术以其独特的魅力,成为了生成高度真实感虚拟人脸的强大引擎。无论是影视制作中虚拟角色的塑造,还是游戏领域中多样化角色形象的构建…...
常用工具类——Collections集合框架
常用工具类——Collections集合框架 Collections 是 JDK 提供的一个工具类,提供了一系列静态方法,分类来复习! 1.排序操作 reverse(List list) :反转顺序shuffle(List list) : 洗牌,将顺序打乱sort(List list) &…...
Verilog语言学习总结
Verilog语言学习! 目录 文章目录 前言 一、Verilog语言是什么? 1.1 Verilog简介 1.2 Verilog 和 C 的区别 1.3 Verilog 学习 二、Verilog基础知识 2.1 Verilog 的逻辑值 2.2 数字进制 2.3 Verilog标识符 2.4 Verilog 的数据类型 2.4.1 寄存器类型 2.4.2 …...
软件工程-数据流图DFD
数据流图(DFD)是一种图形化技术,它描绘信息流和数据从输入移动到输出的过程中经受的变换。 数据流图是系统逻辑功能和图形表示,即使不是专业的计算机人员也容易理解它,因此是分析员与用户之间极好的通信工具。 设计数…...
为什么需要同时重写equals方法和hashCode方法
在 Java 编程中,equals 和 hashCode 是两个非常重要的方法,它们用于确定对象的相等性和哈希值。这两个方法通常需要同时重写,否则会导致哈希表类(如 HashMap、HashSet)的行为异常。因此,理解这两个方法的工…...
c++11总结26——std::regex
std::regex 是 C11 引入的 正则表达式库,用于 字符串匹配、搜索和替换。 🔹 头文件:#include <regex> 🔹 命名空间:std 🔹 支持的匹配模式:ECMAScript(默认)、POS…...
Linux运维——查看命令帮助信息
查看命令帮助信息 一、查看 Linux 命令帮助信息的要点二、常见命令用法2.1、help2.2、whatis2.3、info2.4、which2.5、whereis2.6、man 一、查看 Linux 命令帮助信息的要点 查看 Shell 内部命令的帮助信息 - 使用 help查看命令的简要说明 - 使用 whatis查看命令的详细说明 - 使…...
应急场景中的数据融合与对齐
1. 概述 在应急管理中,快速、准确地掌握现场状况、实时监控灾情并进行决策至关重要。各类数据(如卫星影像、无人机图像、激光雷达点云、地理信息系统(GIS)数据、传感器数据、社交媒体信息、移动终端数据等)具有来源广泛、格式多样、时空特性不同等特点。如何将这些异构数…...
Java数据结构与算法之“树”
目录 一、什么是树 编辑 二、树的相关组成 1. 常用名词 2.需要了解的名词 三、树的分类 (一)初级树 1.普通树 2.二叉树 (二)中级树 1.哈夫曼树HuffmanTree 2.二叉搜索树BST 3.平衡二叉树AVL (三&#x…...
网络HTTP详细讲解
学习目标 什么是HTTPHTTP的请求和响应常见的HTTP状态码HTTP的安全性 什么是HTTP?HTTP的请求和响应,常见的HTTP状态码,HTTP的安全性 什么是HTTP HTTP(HyperText Transfer Protocol,超文本传输协议)是一种用…...
基于Python的智能物流路径优化算法研究与应用
基于Python的智能物流路径优化算法研究与应用 摘要 随着电商行业的迅猛发展,物流配送的效率和成本成为影响企业竞争力的关键因素。本论文聚焦于基于Python语言实现智能物流路径优化算法的研究。通过对经典路径优化算法如Dijkstra算法、A*算法等的深入分析ÿ…...
