feat: update package version and dependencies; add ReconnectingWebSocket for automatic reconnection

This commit is contained in:
2026-02-02 21:22:49 +08:00
parent b081a03399
commit 7f7ea79689
5 changed files with 308 additions and 51 deletions

65
src/ws.ts Normal file
View File

@@ -0,0 +1,65 @@
import { ReconnectingWebSocket, ReconnectConfig } from "./server/reconnect-ws.ts";
export * from "./server/reconnect-ws.ts";
import type { App } from "./app.ts";
export const handleCallWsApp = async (ws: ReconnectingWebSocket, app: App, message: any) => {
return handleCallApp((data: any) => {
ws.send(data);
}, app, message);
}
export const handleCallApp = async (send: (data: any) => void, app: App, message: any) => {
if (message.type === 'router' && message.id) {
const data = message?.data;
if (!message.id) {
console.error('Message id is required for router type');
return;
}
if (!data) {
send({
type: 'router',
id: message.id,
data: { code: 500, message: 'No data received' }
});
return;
}
const { tokenUser, ...rest } = data || {};
const res = await app.run(rest, {
state: { tokenUser },
appId: app.appId,
});
send({
type: 'router',
id: message.id,
data: res
});
}
}
export class Ws {
wsClient: ReconnectingWebSocket;
app: App;
showLog: boolean = true;
constructor(opts?: ReconnectConfig & {
url: string;
app: App;
showLog?: boolean;
handleMessage?: (ws: ReconnectingWebSocket, app: App, message: any) => void;
}) {
const { url, app, showLog = true, handleMessage = handleCallWsApp, ...rest } = opts;
this.wsClient = new ReconnectingWebSocket(url, rest);
this.app = app;
this.showLog = showLog;
this.wsClient.connect();
const onMessage = async (data: any) => {
return handleMessage(this.wsClient, this.app, data);
}
this.wsClient.onMessage(onMessage);
}
send(data: any): boolean {
return this.wsClient.send(data);
}
log(...args: any[]): void {
if (this.showLog)
console.log('[Ws]', ...args);
}
}