**@esengine/server** - 游戏服务器框架 | Game server framework - 文件路由系统 | File-based routing - Room 生命周期 (onCreate, onJoin, onLeave, onTick, onDispose) - @onMessage 装饰器 | Message handler decorator - 玩家管理与断线处理 | Player management with auto-disconnect - 内置 JoinRoom/LeaveRoom API | Built-in room APIs - defineApi/defineMsg 类型安全辅助函数 | Type-safe helpers **create-esengine-server** - CLI 脚手架工具 | CLI scaffolding - 生成 shared/server/client 项目结构 | Project structure - 类型安全的协议定义 | Type-safe protocol definitions - 包含 GameRoom 示例 | Example implementation **其他 | Other** - 删除旧的 network-server 包 | Remove old network-server - 更新服务器文档 | Update server documentation
52 lines
1.2 KiB
TypeScript
52 lines
1.2 KiB
TypeScript
/**
|
|
* @zh API 和消息定义助手
|
|
* @en API and message definition helpers
|
|
*/
|
|
|
|
import type { ApiDefinition, MsgDefinition } from '../types/index.js'
|
|
|
|
/**
|
|
* @zh 定义 API 处理器
|
|
* @en Define API handler
|
|
*
|
|
* @example
|
|
* ```typescript
|
|
* // src/api/join.ts
|
|
* import { defineApi } from '@esengine/server'
|
|
*
|
|
* export default defineApi<ReqJoin, ResJoin>({
|
|
* handler(req, ctx) {
|
|
* ctx.conn.data.playerId = generateId()
|
|
* return { playerId: ctx.conn.data.playerId }
|
|
* }
|
|
* })
|
|
* ```
|
|
*/
|
|
export function defineApi<TReq, TRes, TData = Record<string, unknown>>(
|
|
definition: ApiDefinition<TReq, TRes, TData>
|
|
): ApiDefinition<TReq, TRes, TData> {
|
|
return definition
|
|
}
|
|
|
|
/**
|
|
* @zh 定义消息处理器
|
|
* @en Define message handler
|
|
*
|
|
* @example
|
|
* ```typescript
|
|
* // src/msg/input.ts
|
|
* import { defineMsg } from '@esengine/server'
|
|
*
|
|
* export default defineMsg<MsgInput>({
|
|
* handler(msg, ctx) {
|
|
* console.log('Input from', ctx.conn.id, msg)
|
|
* }
|
|
* })
|
|
* ```
|
|
*/
|
|
export function defineMsg<TMsg, TData = Record<string, unknown>>(
|
|
definition: MsgDefinition<TMsg, TData>
|
|
): MsgDefinition<TMsg, TData> {
|
|
return definition
|
|
}
|