# phoenix-plugin-client **Repository Path**: wuzhouhai/phoenix-plugin-client ## Basic Information - **Project Name**: phoenix-plugin-client - **Description**: phoenix服务器配套cocoscreator3.8.xSDK - **Primary Language**: TypeScript - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-07-22 - **Last Updated**: 2025-08-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 项目简介 phoenix-plugin-client 是一个 phoenix 插件,用于与phoenix服务器框架进行通信。 ## 开发环境 CocosCreator 3.8.3 + ## 安装 ```bash # 安装依赖模块 npm install # 构建 npm run build ``` 克隆此仓库后,在项目根目录创建文件夹extensions/phoenix-plugin-client,将插件源码放置到目录下 ## assets/core文件夹说明 ### defines - error.define.ts 定义错误码,中心web服务器返回错误信息 - error.local.define.ts 定义错误码,插件本地错误信息,与服务器无关 - event.define.ts 定义事件,插件会触发的时间,使用者可以监听 - opcode.define.ts 定义操作码,插件会发送给游戏服务器的请求码,长连接使用 - platform.define.ts 定义平台类型,插件会根据平台类型进行一些操作 ### dto - 与中心web服务器通信的数据传输对象 - 与游戏web服务器通信的数据传输对象 ### entites 实体信息 ### http http请求相关 ### interface 接口信息 ### managers 管理器信息 - message.manager.ts 消息管理器(长连接) ### net 网络相关(长连接) ### platform 平台操作封装 - platform.phoenix.ts phoenix平台操作 - platformManager.ts 平台管理器 ### protoc protobuf相关 ### response web服务器响应信息 ### utils 工具类 ## 类型说明 ### Phoenix.ts 插件核心 ### PhoenixRoot.ts 插件对外接口 ## 用法(建议) ### 初始化 插件使用之前必须执行InitPhoeix()方法进行初始化 ### 角色数据模型 下方的AvatarModel就是自定义角色模型类,存储角色数据信息,插件会在上传保存数据时序列化传输到服务器保存,获取数据时会反序列化到对象,填充数据 必须实现IModelData接口 ``` export class AvatarModel implements IModelData{ gamePoint:number; constructor(){ this.gamePoint = 0; } SerializeData(): string { // 在这里处理上传到服务器的数据,转换为string类型 return JSON.stringify(this); } Deserialization(data: string): void { let d = JSON.parse(data); console.log(d); // 在这里处理数据还原 } } ``` ### 短连接 1. 编写PhoenixClient类继承PhoenixRoot,将PhoenixClient挂接到项目根节点下,在PhoenixClient类中编写相关逻辑 2. 需要绑定角色模型数据,在PhoenixClient类start中调用bindAvatarModel ``` start(): void { super.start(); // 绑定角色数据模型 this.bindAvatarModel(new AvatarModel()); } ``` 3. 服务器信息设置,在挂接脚本之后需要在面板中设置中心服务器信息 ![这是图片](./img/serverConfig.png) 4. 登录 在PhoenixClient类中调用,platformLogin,传入账号和密码,如果是非phoenix平台,账号密码留空 ``` this.platformLogin(account, passwd) ``` 5. 监听登录成功和失败消息 ``` onLoad() { // 注册phoenix事件 this.registerEvent(PhoenixEventDefine.PHOENIX_LOGIN_SUCCESS, this.onLoginSuccess, this); this.registerEvent(PhoenixEventDefine.PHOENIX_LOGIN_FAIL, this.onLoginFail, this); } ``` 6. 更新角色信息 在PhoenixClient类中调用 updateAvatar 7. 获取服务器时间 在PhoenixClient类中调用 updateServerTimer 然后调用 getServerTime 获取查询到的服务器时间 8. token 游戏web服务器登录成功之后,会有token返回,以及token过期时间,在过期时间快到的时候需要调用getToken重新获取token ### 长连接(待续)