feat(wip): 進一步完善 Client 與服務端的交互邏輯
This commit is contained in:
14
client/api/ApiCallbackMessage.ts
Normal file
14
client/api/ApiCallbackMessage.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
type ApiCallbackMessage = {
|
||||||
|
msg: string,
|
||||||
|
/**
|
||||||
|
* 200: 成功
|
||||||
|
* 400: 伺服器端無法理解客戶端請求
|
||||||
|
* 401: 需要身份驗證
|
||||||
|
* 403: 伺服器端拒絕執行客戶端請求
|
||||||
|
* 404: Not Found
|
||||||
|
* 500: 伺服器端錯誤
|
||||||
|
* 501: 伺服器端不支持請求的功能
|
||||||
|
*/
|
||||||
|
code: 200 | 400 | 401 | 403 | 404 | 500 | 501,
|
||||||
|
}
|
||||||
|
export default ApiCallbackMessage
|
||||||
3
client/api/ApiDeclare.ts
Normal file
3
client/api/ApiDeclare.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
export type CallMethod =
|
||||||
|
"User.register" |
|
||||||
|
"User.login"
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
import { io, Socket } from 'https://unpkg.com/socket.io-client@4.8.1/dist/socket.io.esm.min.js'
|
import { io, Socket } from 'https://unpkg.com/socket.io-client@4.8.1/dist/socket.io.esm.min.js'
|
||||||
|
import { CallMethod } from './ApiDeclare.ts'
|
||||||
|
import ApiCallbackMessage from './ApiCallbackMessage.ts'
|
||||||
|
|
||||||
class Client {
|
class Client {
|
||||||
static socket: Socket
|
static socket: Socket
|
||||||
@@ -6,6 +8,14 @@ class Client {
|
|||||||
this.socket && this.socket.disconnect()
|
this.socket && this.socket.disconnect()
|
||||||
this.socket = io()
|
this.socket = io()
|
||||||
}
|
}
|
||||||
|
static call(method: CallMethod, args: {}, timeout: number = 5000) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.socket.timeout().emit("The_White_Silk", (err, res: ApiCallbackMessage) => {
|
||||||
|
if (err) return reject(err)
|
||||||
|
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Client
|
export default Client
|
||||||
|
|||||||
Reference in New Issue
Block a user