chore: 客戶端發送非驗證性請求前, 必須先等待驗證

This commit is contained in:
CrescentLeaf
2025-09-24 21:52:53 +08:00
parent 954b5d3430
commit 9a3e87d89c
2 changed files with 13 additions and 3 deletions

View File

@@ -19,5 +19,11 @@ export type CallMethod =
"Chat.uploadFile" "Chat.uploadFile"
export const CallableMethodBeforeAuth = [
"User.auth",
"User.register",
"User.login",
]
export type ClientEvent = export type ClientEvent =
"Client.onMessage" "Client.onMessage"

View File

@@ -1,5 +1,5 @@
import { io, Socket } from 'socket.io-client' 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 ApiCallbackMessage from './ApiCallbackMessage.ts'
import User from "./client_data/User.ts" import User from "./client_data/User.ts"
import data from "../Data.ts" import data from "../Data.ts"
@@ -12,6 +12,7 @@ class Client {
static myUserProfile?: User static myUserProfile?: User
static socket?: Socket static socket?: Socket
static events: { [key: string]: (data: UnknownObject) => UnknownObject | void } = {} static events: { [key: string]: (data: UnknownObject) => UnknownObject | void } = {}
static connected = false
static connect() { static connect() {
if (data.device_id == null) if (data.device_id == null)
data.device_id = randomUUID() data.device_id = randomUUID()
@@ -29,6 +30,10 @@ class Client {
}, 1000) }, 1000)
if (re.code != 200) if (re.code != 200)
checkApiSuccessOrSncakbar(re, "重連失敗") 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) => { this.socket!.on("The_White_Silk", (name: string, data: UnknownObject, callback: (ret: UnknownObject) => void) => {
try { try {
@@ -41,8 +46,7 @@ class Client {
}) })
} }
static invoke(method: CallMethod, args: UnknownObject = {}, timeout: number = 5000): Promise<ApiCallbackMessage> { static invoke(method: CallMethod, args: UnknownObject = {}, timeout: number = 5000): Promise<ApiCallbackMessage> {
if (this.socket == null) { if (this.socket == null || (!this.connected && !CallableMethodBeforeAuth.includes(method))) {
console.warn("客戶端未初始化, 等待初始化后再請求......")
return new Promise((reslove) => { return new Promise((reslove) => {
setTimeout(async () => reslove(await this.invoke(method, args, timeout)), 500) setTimeout(async () => reslove(await this.invoke(method, args, timeout)), 500)
}) })