chore: 修繕客戶端請求允許等待連接

This commit is contained in:
CrescentLeaf
2025-09-21 16:12:31 +08:00
parent cb947429fb
commit f118c6b6f5
2 changed files with 10 additions and 6 deletions

View File

@@ -8,8 +8,9 @@ type ApiCallbackMessage = {
* 404: Not Found
* 500: 伺服器端錯誤
* 501: 伺服器端不支持請求的功能
* -1: 客戶端錯誤
*/
code: 200 | 400 | 401 | 403 | 404 | 500 | 501,
data?: { [key: string]: unknown },
code: 200 | 400 | 401 | 403 | 404 | 500 | 501 | -1
data?: { [key: string]: unknown }
}
export default ApiCallbackMessage

View File

@@ -9,7 +9,7 @@ type UnknownObject = { [key: string]: unknown }
class Client {
static myUserProfile?: User
static socket?: Socket
static events: { [key: string]: (data: UnknownObject) => UnknownObject | undefined } = {}
static events: { [key: string]: (data: UnknownObject) => UnknownObject | void } = {}
static connect() {
if (data.device_id == null)
data.device_id = crypto.randomUUID()
@@ -37,9 +37,12 @@ class Client {
setTimeout(async () => reslove(await this.invoke(method, args, timeout)), 500)
})
}
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
this.socket!.timeout(timeout).emit("The_White_Silk", method, args, (err: string, res: ApiCallbackMessage) => {
if (err) return reject(err)
if (err) return resolve({
code: -1,
msg: err,
})
resolve(res)
})
})
@@ -57,7 +60,7 @@ class Client {
token: data.access_token
})).data as unknown as User
}
static on(eventName: ClientEvent, func: (data: UnknownObject) => UnknownObject) {
static on(eventName: ClientEvent, func: (data: UnknownObject) => UnknownObject | void) {
this.events[eventName] = func
}
static off(eventName: ClientEvent) {