diff --git a/client/api/ApiCallbackMessage.ts b/client/api/ApiCallbackMessage.ts new file mode 100644 index 0000000..185651b --- /dev/null +++ b/client/api/ApiCallbackMessage.ts @@ -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 diff --git a/client/api/ApiDeclare.ts b/client/api/ApiDeclare.ts new file mode 100644 index 0000000..a907e67 --- /dev/null +++ b/client/api/ApiDeclare.ts @@ -0,0 +1,3 @@ +export type CallMethod = + "User.register" | + "User.login" diff --git a/client/api/Client.ts b/client/api/Client.ts index 983ea07..55a7d14 100644 --- a/client/api/Client.ts +++ b/client/api/Client.ts @@ -1,4 +1,6 @@ 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 { static socket: Socket @@ -6,6 +8,14 @@ class Client { this.socket && this.socket.disconnect() 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