This commit is contained in:
yanghang
2025-07-09 14:16:51 +08:00
parent f14ae6d5ba
commit c3aea146db
7 changed files with 44 additions and 4 deletions
+3
View File
@@ -20,6 +20,7 @@ export default class WebSocketUtil {
this.heartbeatTimer = null; // 心跳计时器
this.heartbeatInterval = options.heartbeatInterval || 30000; // 心跳间隔(ms)
this.heartbeatMsg = options.heartbeatMsg || 'ping'; // 心跳消息
this.isClose = false; // 手动关闭
this.callbacks = {
open: [], // 连接打开回调
message: [], // 消息接收回调
@@ -165,6 +166,7 @@ export default class WebSocketUtil {
* @returns {WebSocketUtil} - 返回当前实例,支持链式调用
*/
close(code = 1000, reason = '') {
this.isClose = true
if (this.socketTask) {
clearInterval(this.heartbeatTimer); // 清除心跳计时器
clearInterval(this.reconnectTimer); // 清除重连计时器
@@ -204,6 +206,7 @@ export default class WebSocketUtil {
* 尝试重新连接WebSocket
*/
tryReconnect() {
if(this.isClose) return
if (this.reconnectCount < this.maxReconnectCount) {
clearInterval(this.reconnectTimer);
this.reconnectTimer = setTimeout(() => {