From 9a3e87d89c277abfc352550c1a1ddc45293331d9 Mon Sep 17 00:00:00 2001 From: CrescentLeaf Date: Wed, 24 Sep 2025 21:52:53 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E5=AE=A2=E6=88=B6=E7=AB=AF=E7=99=BC?= =?UTF-8?q?=E9=80=81=E9=9D=9E=E9=A9=97=E8=AD=89=E6=80=A7=E8=AB=8B=E6=B1=82?= =?UTF-8?q?=E5=89=8D,=20=E5=BF=85=E9=A0=88=E5=85=88=E7=AD=89=E5=BE=85?= =?UTF-8?q?=E9=A9=97=E8=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/api/ApiDeclare.ts | 6 ++++++ client/api/Client.ts | 10 +++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/client/api/ApiDeclare.ts b/client/api/ApiDeclare.ts index 4edba17..1b70459 100644 --- a/client/api/ApiDeclare.ts +++ b/client/api/ApiDeclare.ts @@ -19,5 +19,11 @@ export type CallMethod = "Chat.uploadFile" +export const CallableMethodBeforeAuth = [ + "User.auth", + "User.register", + "User.login", +] + export type ClientEvent = "Client.onMessage" diff --git a/client/api/Client.ts b/client/api/Client.ts index 108b8ec..76260f1 100644 --- a/client/api/Client.ts +++ b/client/api/Client.ts @@ -1,5 +1,5 @@ import { io, Socket } from 'socket.io-client' -import { CallMethod, ClientEvent } from './ApiDeclare.ts' +import { CallMethod, ClientEvent, CallableMethodBeforeAuth } from './ApiDeclare.ts' import ApiCallbackMessage from './ApiCallbackMessage.ts' import User from "./client_data/User.ts" import data from "../Data.ts" @@ -12,6 +12,7 @@ class Client { static myUserProfile?: User static socket?: Socket static events: { [key: string]: (data: UnknownObject) => UnknownObject | void } = {} + static connected = false static connect() { if (data.device_id == null) data.device_id = randomUUID() @@ -29,6 +30,10 @@ class Client { }, 1000) if (re.code != 200) checkApiSuccessOrSncakbar(re, "重連失敗") + this.connected = true + }) + this.socket!.on("disconnect", () => { + this.connected = false }) this.socket!.on("The_White_Silk", (name: string, data: UnknownObject, callback: (ret: UnknownObject) => void) => { try { @@ -41,8 +46,7 @@ class Client { }) } static invoke(method: CallMethod, args: UnknownObject = {}, timeout: number = 5000): Promise { - if (this.socket == null) { - console.warn("客戶端未初始化, 等待初始化后再請求......") + if (this.socket == null || (!this.connected && !CallableMethodBeforeAuth.includes(method))) { return new Promise((reslove) => { setTimeout(async () => reslove(await this.invoke(method, args, timeout)), 500) })