websocket全局封装使用

WebSocket对象的创建
WebSocket对象的关闭
启用心跳机制,避免断连
消息推送,接收到消息后进行业务逻辑处理
重连机制,如果断连后尝试一定次数的重连,超过最大次数后仍然失败则关闭连接
调用案例如下:
const socketManager = new WebSocketManager(‘ws://example.com/socket’, receiveMessage)
socketManager.start()

export default class WebSocketManager {
	constructor(url = null, receiveMessageCallback = null) {
		this.socket = null // WebSocket 对象
		this.pingTimeout = null // 心跳计时器
		this.reconnectTimeout = 5000 // 重连间隔,单位:毫秒
		this.maxReconnectAttempts = 10 // 最大重连尝试次数
		this.reconnectAttempts = 0; // 当前重连尝试次数
		this.url = url // WebSocket 连接地址
		this.receiveMessageCallback = receiveMessageCallback // 接收消息回调函数
	}
	/**
		 * 初始化
		 */
	async start() {
		if (this.url) {
			// 连接WebSocket
			this.connectWebSocket()
		} else {
			console.error('WebSocketManager erros: 请传入连接地址')
		}
	}
	/**
		 * 创建WebSocket连接
		 */
	connectWebSocket() {
		// 创建 WebSocket 对象
		this.socket = new WebSocket(this.url)

		// 处理连接打开事件
		this.socket.addEventListener('open', event => {
			// 心跳机制
			this.startHeartbeat()
		})

		// 处理接收到消息事件
		this.socket.addEventListener('message', event => {
			this.receiveMessage(event)
		})

		// 处理连接关闭事件
		this.socket.addEventListener('close', event => {
			// 清除定时器
			clearTimeout(this.pingTimeout)
			clearTimeout(this.reconnectTimeout)
			// 尝试重连
			if (this.reconnectAttempts < this.maxReconnectAttempts) {
				this.reconnectAttempts++
				this.reconnectTimeout = setTimeout(() => {
					this.connectWebSocket()
				}, this.reconnectTimeout)
			} else {
				// 重置重连次数
				this.reconnectAttempts = 0
				console.error('已达到最大重新连接尝试次数。无法重新连接。')
			}
		})

		// 处理 WebSocket 错误事件
		this.socket.addEventListener('error', event => {
			console.error('WebSocketManager error:', event)
		})
	}
	/**
		 * 启动心跳机制
		 */
	startHeartbeat() {
		this.pingTimeout = setInterval(() => {
			// 发送心跳消息
			this.sendMessage('ping')
		}, 10000) // 每隔 10 秒发送一次心跳
	}

	/**
	 * 发送消息
	 * @param {String} message 消息内容
	 */
	sendMessage(message) {
		if (this.socket.readyState === WebSocket.OPEN) {
			this.socket.send(message);
		} else {
			console.error('WebSocketManager error: WebSocket连接未打开,无法发送消息。')
		}
	}
	/**
		 * 接收到消息
		 */
	receiveMessage(event) {
		// 根据业务自行处理
		console.log('receiveMessage:', event.data)
		// 传入回调函数自行处理
		this.receiveMessageCallback && receiveMessageCallback(event.data)
	}
	/**
		 * 关闭连接
		 */
	closeWebSocket() {
		this.socket.close()
		// 清除定时器 重置重连次数
		clearTimeout(this.pingTimeout)
		clearTimeout(this.reconnectTimeout)
		this.reconnectAttempts = 0
	}
}

相关推荐

  1. websocket全局封装使用

    2024-04-30 16:02:01       28 阅读
  2. electron-egg webSocket使用封装

    2024-04-30 16:02:01       23 阅读
  3. Flutter: Websocket使用封装

    2024-04-30 16:02:01       53 阅读
  4. vue3:websocket封装使用

    2024-04-30 16:02:01       51 阅读
  5. Vue websocket封装使用

    2024-04-30 16:02:01       53 阅读
  6. 在vue项目中封装使用WebSocket(2)

    2024-04-30 16:02:01       45 阅读
  7. 在vue和uniapp中使用 websocket封装js

    2024-04-30 16:02:01       33 阅读
  8. ts + websocket封装

    2024-04-30 16:02:01       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-30 16:02:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-30 16:02:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-30 16:02:01       82 阅读
  4. Python语言-面向对象

    2024-04-30 16:02:01       91 阅读

热门阅读

  1. echarts地图绘制

    2024-04-30 16:02:01       30 阅读
  2. 【软测学习笔记】Linux入门Day02

    2024-04-30 16:02:01       31 阅读
  3. unix 命令总结

    2024-04-30 16:02:01       26 阅读
  4. 安卓adb 命令查看程序日志

    2024-04-30 16:02:01       31 阅读
  5. 使用ldirectord实现LVS健康检测

    2024-04-30 16:02:01       35 阅读
  6. 更通用的excel公式转python代码方法

    2024-04-30 16:02:01       32 阅读
  7. 查找子串第一次出现的位置(头歌)

    2024-04-30 16:02:01       24 阅读
  8. Linux学习_09-Linux的用户管理

    2024-04-30 16:02:01       24 阅读