chore: 修繕客戶端請求允許等待連接
This commit is contained in:
@@ -8,8 +8,9 @@ type ApiCallbackMessage = {
|
|||||||
* 404: Not Found
|
* 404: Not Found
|
||||||
* 500: 伺服器端錯誤
|
* 500: 伺服器端錯誤
|
||||||
* 501: 伺服器端不支持請求的功能
|
* 501: 伺服器端不支持請求的功能
|
||||||
|
* -1: 客戶端錯誤
|
||||||
*/
|
*/
|
||||||
code: 200 | 400 | 401 | 403 | 404 | 500 | 501,
|
code: 200 | 400 | 401 | 403 | 404 | 500 | 501 | -1
|
||||||
data?: { [key: string]: unknown },
|
data?: { [key: string]: unknown }
|
||||||
}
|
}
|
||||||
export default ApiCallbackMessage
|
export default ApiCallbackMessage
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ type UnknownObject = { [key: string]: unknown }
|
|||||||
class Client {
|
class Client {
|
||||||
static myUserProfile?: User
|
static myUserProfile?: User
|
||||||
static socket?: Socket
|
static socket?: Socket
|
||||||
static events: { [key: string]: (data: UnknownObject) => UnknownObject | undefined } = {}
|
static events: { [key: string]: (data: UnknownObject) => UnknownObject | void } = {}
|
||||||
static connect() {
|
static connect() {
|
||||||
if (data.device_id == null)
|
if (data.device_id == null)
|
||||||
data.device_id = crypto.randomUUID()
|
data.device_id = crypto.randomUUID()
|
||||||
@@ -37,9 +37,12 @@ class Client {
|
|||||||
setTimeout(async () => reslove(await this.invoke(method, args, timeout)), 500)
|
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) => {
|
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)
|
resolve(res)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -57,7 +60,7 @@ class Client {
|
|||||||
token: data.access_token
|
token: data.access_token
|
||||||
})).data as unknown as User
|
})).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
|
this.events[eventName] = func
|
||||||
}
|
}
|
||||||
static off(eventName: ClientEvent) {
|
static off(eventName: ClientEvent) {
|
||||||
|
|||||||
Reference in New Issue
Block a user