Files
esengine/packages/framework/network/src/protocol.ts

181 lines
3.5 KiB
TypeScript
Raw Normal View History

feat(rpc,network): 新增 RPC 库并迁移网络模块 (#364) * feat(rpc,network): 新增 RPC 库并迁移网络模块 ## @esengine/rpc (新增) - 新增类型安全的 RPC 库,支持 WebSocket 通信 - 新增 RpcClient 类:connect/disconnect, call/send/on/off/once 方法 - 新增 RpcServer 类:Node.js WebSocket 服务端 - 新增编解码系统:支持 JSON 和 MessagePack - 新增 TextEncoder/TextDecoder polyfill,兼容微信小游戏平台 - 新增 WebSocketAdapter 接口,支持跨平台 WebSocket 抽象 ## @esengine/network (重构) - 重构 NetworkService:拆分为 RpcService 基类和 GameNetworkService - 新增 gameProtocol:类型安全的 API 和消息定义 - 新增类型安全便捷方法:sendInput(), onSync(), onSpawn(), onDespawn() - 更新 NetworkPlugin 使用新的服务架构 - 移除 TSRPC 依赖,改用 @esengine/rpc ## 文档 - 新增 RPC 模块文档(中英文) - 更新 Network 模块文档(中英文) - 更新侧边栏导航 * fix(network,cli): 修复 CI 构建和更新 CLI 适配器 ## 修复 - 在 tsconfig.build.json 添加 rpc 引用,修复类型声明生成 ## CLI 更新 - 更新 nodejs 适配器使用新的 @esengine/rpc - 生成的服务器代码使用 RpcServer 替代旧的 GameServer - 添加 ws 和 @types/ws 依赖 - 更新 README 模板中的客户端连接示例 * chore: 添加 CLI changeset * fix(ci): add @esengine/rpc to build and check scripts - Add rpc package to CI build step (must build before network) - Add rpc to type-check:framework, lint:framework, test:ci:framework * fix(rpc,network): fix tsconfig for declaration generation - Remove composite mode from rpc (not needed, causes CI issues) - Remove rpc from network project references (resolves via node_modules) - Remove unused references from network tsconfig.build.json
2025-12-28 10:54:51 +08:00
/**
* @zh
* @en Game Network Protocol Definition
*
* @zh
* @en Defines the communication protocol between client and server
*/
import { rpc } from '@esengine/rpc'
// ============================================================================
// Message Types | 消息类型
// ============================================================================
/**
* @zh
* @en Player input
*/
export interface PlayerInput {
/**
* @zh
* @en Frame number
*/
frame: number
/**
* @zh
* @en Move direction
*/
moveDir?: { x: number; y: number }
/**
* @zh
* @en Action list
*/
actions?: string[]
}
/**
* @zh
* @en Entity sync state
*/
export interface EntitySyncState {
netId: number
pos?: { x: number; y: number }
rot?: number
}
/**
* @zh
* @en Sync message
*/
export interface SyncData {
/**
* @zh
* @en Server frame number
*/
frame: number
/**
* @zh
* @en Entity state list
*/
entities: EntitySyncState[]
}
/**
* @zh
* @en Spawn message
*/
export interface SpawnData {
netId: number
ownerId: number
prefab: string
pos: { x: number; y: number }
rot?: number
}
/**
* @zh
* @en Despawn message
*/
export interface DespawnData {
netId: number
}
// ============================================================================
// API Types | API 类型
// ============================================================================
/**
* @zh
* @en Join room request
*/
export interface JoinRequest {
playerName: string
roomId?: string
}
/**
* @zh
* @en Join room response
*/
export interface JoinResponse {
playerId: number
roomId: string
}
// ============================================================================
// Protocol Definition | 协议定义
// ============================================================================
/**
* @zh
* @en Default game network protocol
*
* @example
* ```typescript
* // 使用默认协议
* const service = new NetworkService(gameProtocol)
*
* // 或者扩展协议
* const customProtocol = rpc.define({
* api: {
* ...gameProtocol.api,
* customApi: rpc.api<CustomInput, CustomOutput>(),
* },
* msg: {
* ...gameProtocol.msg,
* customMsg: rpc.msg<CustomData>(),
* },
* })
* ```
*/
export const gameProtocol = rpc.define({
api: {
/**
* @zh
* @en Join room
*/
join: rpc.api<JoinRequest, JoinResponse>(),
/**
* @zh
* @en Leave room
*/
leave: rpc.api<void, void>(),
},
msg: {
/**
* @zh
* @en Player input
*/
input: rpc.msg<PlayerInput>(),
/**
* @zh
* @en State sync
*/
sync: rpc.msg<SyncData>(),
/**
* @zh
* @en Entity spawn
*/
spawn: rpc.msg<SpawnData>(),
/**
* @zh
* @en Entity despawn
*/
despawn: rpc.msg<DespawnData>(),
},
})
/**
* @zh
* @en Game protocol type
*/
export type GameProtocol = typeof gameProtocol