feat: 添加工作空间保持存活功能,更新相关依赖和配置

This commit is contained in:
2026-01-30 23:32:40 +08:00
parent 972d68b87e
commit 0d17d56628
12 changed files with 394 additions and 29 deletions

View File

@@ -12,6 +12,7 @@ export interface KeepAliveConfig {
onDisconnect?: (code: number) => void;
onError?: (error: Error) => void;
onSign?: (data: { type: string; data: string; signedData: string }) => void;
onExit?: (code?: number) => void;
debug?: boolean;
}
@@ -38,11 +39,12 @@ export class WSKeepAlive {
reconnectInterval: config.reconnectInterval ?? 5000,
maxReconnectAttempts: config.maxReconnectAttempts ?? 3,
pingInterval: config.pingInterval ?? 30000,
onMessage: config.onMessage ?? (() => {}),
onConnect: config.onConnect ?? (() => {}),
onDisconnect: config.onDisconnect ?? (() => {}),
onError: config.onError ?? (() => {}),
onSign: config.onSign ?? (() => {}),
onMessage: config.onMessage ?? (() => { }),
onConnect: config.onConnect ?? (() => { }),
onDisconnect: config.onDisconnect ?? (() => { }),
onError: config.onError ?? (() => { }),
onSign: config.onSign ?? (() => { }),
onExit: config.onExit ?? (() => { }),
debug: config.debug ?? false,
};
this.url = new URL(this.config.wsUrl);
@@ -158,6 +160,7 @@ export class WSKeepAlive {
private handleReconnect() {
if (this.reconnectAttempts >= this.config.maxReconnectAttempts) {
this.log(`Max reconnect attempts (${this.config.maxReconnectAttempts}) reached. Giving up.`);
this.config.onExit(1);
return;
}
this.reconnectAttempts++;
@@ -174,6 +177,7 @@ export class WSKeepAlive {
this.stopPing();
if (this.ws) {
this.ws.close();
this.config.onExit(0);
this.ws = null;
}
}

View File

@@ -0,0 +1,13 @@
import { createKeepAlive } from '@kevisual/cnb/keep';
const wsUrl = process.argv[2];
const cookie = process.argv[3];
createKeepAlive({
wsUrl,
cookie,
onConnect: () => console.log('已连接'),
debug: true
});
process.on('SIGINT', () => process.exit(0));