進一步完善 Server API
This commit is contained in:
@@ -10,5 +10,6 @@ type ApiCallbackMessage = {
|
||||
* 501: 伺服器端不支持請求的功能
|
||||
*/
|
||||
code: 200 | 400 | 401 | 403 | 404 | 500 | 501,
|
||||
data?: { [key: string]: unknown },
|
||||
}
|
||||
export default ApiCallbackMessage
|
||||
7
server/api/ApiDeclare.ts
Normal file
7
server/api/ApiDeclare.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export type CallMethod =
|
||||
"User.auth" |
|
||||
"User.register" |
|
||||
"User.login"
|
||||
|
||||
export type ClientEvent =
|
||||
"Client.onMessage"
|
||||
@@ -1,14 +1,15 @@
|
||||
import HttpServerLike from '../types/HttpServerLike.ts'
|
||||
import UserApi from "./UserApi.ts"
|
||||
import * as SocketIo from "socket.io"
|
||||
import ApiCallbackMessage from "../types/ApiCallbackMessage.ts"
|
||||
import ApiCallbackMessage from "./ApiCallbackMessage.ts"
|
||||
import EventCallbackFunction from "../types/EventCallbackFunction.ts"
|
||||
import BaseApi from "./BaseApi.ts"
|
||||
|
||||
export default class ApiManager {
|
||||
static httpServer: HttpServerLike
|
||||
static socketIoServer: SocketIo.Server
|
||||
static event_listeners: { [key: string]: EventCallbackFunction } = {}
|
||||
static apis_instance: {}
|
||||
static apis_instance: { [key: string]: BaseApi } = {}
|
||||
static initServer(httpServer: HttpServerLike, socketIoServer: SocketIo.Server) {
|
||||
this.httpServer = httpServer
|
||||
this.socketIoServer = socketIoServer
|
||||
@@ -30,13 +31,17 @@ export default class ApiManager {
|
||||
static initEvents() {
|
||||
const io = this.socketIoServer
|
||||
io.on('connection', (socket) => {
|
||||
socket.on("The_White_Silk", (name: string, args: {}, callback: (ret: ApiCallbackMessage) => void) => {
|
||||
socket.on("The_White_Silk", (name: string, args: { [key: string]: unknown }, callback: (ret: ApiCallbackMessage) => void) => {
|
||||
try {
|
||||
if (name == null || args == null) return callback({
|
||||
msg: "Invalid request.",
|
||||
code: 400
|
||||
})
|
||||
|
||||
return callback(this.event_listeners[name]?.(args))
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import EventCallbackFunction from "../types/EventCallbackFunction.ts"
|
||||
import ApiManager from "./ApiManager.ts";
|
||||
import ApiManager from "./ApiManager.ts"
|
||||
import { CallMethod } from './ApiDeclare.ts'
|
||||
|
||||
export default abstract class BaseApi {
|
||||
abstract getName(): string
|
||||
@@ -7,7 +8,8 @@ export default abstract class BaseApi {
|
||||
this.onInit()
|
||||
}
|
||||
abstract onInit(): void
|
||||
registerEvent(name: string, func: EventCallbackFunction) {
|
||||
ApiManager.addEventListener(this.getName() + "." + name, func)
|
||||
registerEvent(name: CallMethod, func: EventCallbackFunction) {
|
||||
if (!name.startsWith(this.getName() + ".")) throw Error("注冊的事件應該與接口集合命名空間相匹配: " + name)
|
||||
ApiManager.addEventListener(name, func)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import BaseApi from "./BaseApi.ts";
|
||||
import BaseApi from "./BaseApi.ts"
|
||||
|
||||
export default class UserApi extends BaseApi {
|
||||
override getName(): string {
|
||||
return "User"
|
||||
}
|
||||
override onInit(): void {
|
||||
this.registerEvent("", () => {
|
||||
this.registerEvent("User.auth", () => {
|
||||
return {
|
||||
msg: "",
|
||||
code: 200,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import ApiCallbackMessage from "./ApiCallbackMessage.ts"
|
||||
import ApiCallbackMessage from "../api/ApiCallbackMessage.ts"
|
||||
|
||||
type EventCallbackFunction = (args: {}) => ApiCallbackMessage
|
||||
|
||||
|
||||
Reference in New Issue
Block a user