调整了subscriber断连的行为

This commit is contained in:
Xu Chang 2025-04-10 15:16:07 +08:00
parent f6e2677309
commit d3b456e495
3 changed files with 89 additions and 118 deletions

View File

@ -56,10 +56,8 @@ export class SubScriber extends Feature {
return;
}
this.socketState = 'connecting';
let optionInited = false;
if (!this.url) {
await this.initSocketPoint();
optionInited = true;
this.count = 0;
}
const url = this.url;
@ -84,6 +82,7 @@ export class SubScriber extends Feature {
*/
socket.on('connect', async () => {
this.socketState = 'connected';
this.count = 0;
this.emit('connect');
socket.off('connect');
socket.on('disconnect', () => {
@ -118,44 +117,35 @@ export class SubScriber extends Feature {
socket.disconnect();
}
});
if (!optionInited) {
socket.on('connect_error', async (err) => {
this.count++;
if (this.count > 50) {
// 可能socket地址改变了刷新重连
this.url = undefined;
}
socket.removeAllListeners();
socket.disconnect();
socket.on('connect_error', async (err) => {
this.count++;
socket.removeAllListeners();
// socket.disconnect();
if (this.count > 50) {
// 可能socket地址改变了刷新重连
this.url = undefined;
this.socket = undefined;
// 清除之前的重连定时器
if (this.reconnectTimer) {
clearInterval(this.reconnectTimer);
}
// 根据重连次数设置不同的重连间隔
if (this.count <= 10) {
this.reconnectInterval = 10 * 1000; // 10秒
}
else if (this.count <= 20) {
this.reconnectInterval = 30 * 1000; // 30秒
}
else if (this.count <= 30) {
this.reconnectInterval = 60 * 1000; // 60秒
}
else if (this.count <= 40) {
this.reconnectInterval = 90 * 1000; // 90秒
}
else {
this.reconnectInterval = 120 * 1000; // 2分钟
}
// 设置新的定时器
this.reconnectTimer = setInterval(async () => {
await this.connect();
// resolve(undefined);
}, this.reconnectInterval);
resolve(undefined);
});
}
}
// 根据重连次数设置不同的重连间隔
if (this.count <= 10) {
this.reconnectInterval = 10 * 1000; // 10秒
}
else if (this.count <= 20) {
this.reconnectInterval = 30 * 1000; // 30秒
}
else if (this.count <= 30) {
this.reconnectInterval = 60 * 1000; // 60秒
}
else if (this.count <= 40) {
this.reconnectInterval = 90 * 1000; // 90秒
}
else {
this.reconnectInterval = 120 * 1000; // 2分钟
}
// 设置新的定时器
this.reconnectTimer = setTimeout(() => this.connect(), this.reconnectInterval);
resolve(undefined);
});
socket.connect();
});
}

View File

@ -60,10 +60,8 @@ class SubScriber extends Feature_1.Feature {
return;
}
this.socketState = 'connecting';
let optionInited = false;
if (!this.url) {
await this.initSocketPoint();
optionInited = true;
this.count = 0;
}
const url = this.url;
@ -88,6 +86,7 @@ class SubScriber extends Feature_1.Feature {
*/
socket.on('connect', async () => {
this.socketState = 'connected';
this.count = 0;
this.emit('connect');
socket.off('connect');
socket.on('disconnect', () => {
@ -122,44 +121,35 @@ class SubScriber extends Feature_1.Feature {
socket.disconnect();
}
});
if (!optionInited) {
socket.on('connect_error', async (err) => {
this.count++;
if (this.count > 50) {
// 可能socket地址改变了刷新重连
this.url = undefined;
}
socket.removeAllListeners();
socket.disconnect();
socket.on('connect_error', async (err) => {
this.count++;
socket.removeAllListeners();
// socket.disconnect();
if (this.count > 50) {
// 可能socket地址改变了刷新重连
this.url = undefined;
this.socket = undefined;
// 清除之前的重连定时器
if (this.reconnectTimer) {
clearInterval(this.reconnectTimer);
}
// 根据重连次数设置不同的重连间隔
if (this.count <= 10) {
this.reconnectInterval = 10 * 1000; // 10秒
}
else if (this.count <= 20) {
this.reconnectInterval = 30 * 1000; // 30秒
}
else if (this.count <= 30) {
this.reconnectInterval = 60 * 1000; // 60秒
}
else if (this.count <= 40) {
this.reconnectInterval = 90 * 1000; // 90秒
}
else {
this.reconnectInterval = 120 * 1000; // 2分钟
}
// 设置新的定时器
this.reconnectTimer = setInterval(async () => {
await this.connect();
// resolve(undefined);
}, this.reconnectInterval);
resolve(undefined);
});
}
}
// 根据重连次数设置不同的重连间隔
if (this.count <= 10) {
this.reconnectInterval = 10 * 1000; // 10秒
}
else if (this.count <= 20) {
this.reconnectInterval = 30 * 1000; // 30秒
}
else if (this.count <= 30) {
this.reconnectInterval = 60 * 1000; // 60秒
}
else if (this.count <= 40) {
this.reconnectInterval = 90 * 1000; // 90秒
}
else {
this.reconnectInterval = 120 * 1000; // 2分钟
}
// 设置新的定时器
this.reconnectTimer = setTimeout(() => this.connect(), this.reconnectInterval);
resolve(undefined);
});
socket.connect();
});
}

View File

@ -70,7 +70,7 @@ export class SubScriber<ED extends EntityDict & BaseEntityDict> extends Feature
this.url = subscribeUrl;
this.path = path;
}
catch(err) {
catch (err) {
this.url = undefined;
this.path = undefined;
throw new OakSocketConnectException();
@ -82,10 +82,8 @@ export class SubScriber<ED extends EntityDict & BaseEntityDict> extends Feature
return;
}
this.socketState = 'connecting';
let optionInited = false;
if (!this.url) {
await this.initSocketPoint();
optionInited = true;
this.count = 0;
}
@ -112,6 +110,7 @@ export class SubScriber<ED extends EntityDict & BaseEntityDict> extends Feature
*/
socket.on('connect', async () => {
this.socketState = 'connected';
this.count = 0;
this.emit('connect');
socket.off('connect');
@ -156,41 +155,33 @@ export class SubScriber<ED extends EntityDict & BaseEntityDict> extends Feature
}
});
if (!optionInited) {
socket.on('connect_error', async (err) => {
this.count++;
if (this.count > 50) {
// 可能socket地址改变了刷新重连
this.url = undefined;
}
socket.removeAllListeners();
socket.disconnect();
socket.on('connect_error', async (err) => {
this.count++;
socket.removeAllListeners();
// socket.disconnect();
if (this.count > 50) {
// 可能socket地址改变了刷新重连
this.url = undefined;
this.socket = undefined;
// 清除之前的重连定时器
if (this.reconnectTimer) {
clearInterval(this.reconnectTimer);
}
// 根据重连次数设置不同的重连间隔
if (this.count <= 10) {
this.reconnectInterval = 10 * 1000; // 10秒
} else if (this.count <= 20) {
this.reconnectInterval = 30 * 1000; // 30秒
} else if (this.count <= 30) {
this.reconnectInterval = 60 * 1000; // 60秒
} else if (this.count <= 40) {
this.reconnectInterval = 90 * 1000; // 90秒
} else {
this.reconnectInterval = 120 * 1000; // 2分钟
}
// 设置新的定时器
this.reconnectTimer = setInterval(async () => {
await this.connect();
// resolve(undefined);
}, this.reconnectInterval);
resolve(undefined);
});
}
}
// 根据重连次数设置不同的重连间隔
if (this.count <= 10) {
this.reconnectInterval = 10 * 1000; // 10秒
} else if (this.count <= 20) {
this.reconnectInterval = 30 * 1000; // 30秒
} else if (this.count <= 30) {
this.reconnectInterval = 60 * 1000; // 60秒
} else if (this.count <= 40) {
this.reconnectInterval = 90 * 1000; // 90秒
} else {
this.reconnectInterval = 120 * 1000; // 2分钟
}
// 设置新的定时器
this.reconnectTimer = setTimeout(() => this.connect(), this.reconnectInterval);
resolve(undefined);
});
socket.connect();
});
@ -204,7 +195,7 @@ export class SubScriber<ED extends EntityDict & BaseEntityDict> extends Feature
if (callback) {
registered.callbacks.push(callback);
}
registered.count ++;
registered.count++;
}
else {
this.eventMap[event] = {
@ -252,7 +243,7 @@ export class SubScriber<ED extends EntityDict & BaseEntityDict> extends Feature
(event) => {
const registered = this.eventMap[event];
assert(registered);
registered.count --;
registered.count--;
if (registered.count > 0) {
if (callback) {
pull(registered.callbacks, callback);
@ -286,7 +277,7 @@ export class SubScriber<ED extends EntityDict & BaseEntityDict> extends Feature
setHalted(halted: boolean) {
this.halted = halted;
if (halted === false && Object.keys(this.eventMap).length > 0) {
if (halted === false && Object.keys(this.eventMap).length > 0) {
this.connect();
}
}