Framework.Net网络管理
This commit is contained in:
116
client/assets/scripts/Framework/Net/NetExample.ts
Normal file
116
client/assets/scripts/Framework/Net/NetExample.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
import { _decorator, Component } from 'cc';
|
||||
import { NetConfig } from './NetConfig';
|
||||
import { NetEvent } from './NetEvent';
|
||||
import { NetManager } from './NetManager';
|
||||
|
||||
const { ccclass } = _decorator;
|
||||
|
||||
/**
|
||||
* 网络管理器使用示例
|
||||
* 演示如何使用 NetManager 连接服务器并进行通信
|
||||
*/
|
||||
@ccclass('NetExample')
|
||||
export class NetExample extends Component {
|
||||
|
||||
start() {
|
||||
// 演示网络管理器的使用
|
||||
this.initNetwork();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化网络
|
||||
*/
|
||||
private async initNetwork() {
|
||||
console.log('=== 网络模块使用示例 ===');
|
||||
|
||||
// 1. 获取网络管理器实例
|
||||
const netManager = NetManager.getInstance();
|
||||
|
||||
// 2. 设置服务协议 (必须在 init 之前调用)
|
||||
// TODO: 运行 npm run sync-shared 后,从 Shared 目录导入协议
|
||||
// import { serviceProto } from '../../Shared/protocols/serviceProto';
|
||||
// netManager.setServiceProto(serviceProto);
|
||||
console.log('⚠️ 提示: 请先运行 npm run sync-shared 同步服务端协议');
|
||||
console.log('⚠️ 然后导入 serviceProto 并调用 setServiceProto()');
|
||||
|
||||
// 3. 监听网络事件
|
||||
netManager.on(NetEvent.Connected, () => {
|
||||
console.log('✅ 网络已连接');
|
||||
this.onConnected();
|
||||
});
|
||||
|
||||
netManager.on(NetEvent.Disconnected, () => {
|
||||
console.log('❌ 网络已断开');
|
||||
});
|
||||
|
||||
netManager.on(NetEvent.Reconnecting, (count: number) => {
|
||||
console.log(`🔄 正在重连... (${count})`);
|
||||
});
|
||||
|
||||
netManager.on(NetEvent.Error, (error: any) => {
|
||||
console.error('⚠️ 网络错误:', error);
|
||||
});
|
||||
|
||||
// 4. 配置网络参数
|
||||
const config: NetConfig = {
|
||||
serverUrl: 'http://localhost:3000', // TODO: 替换为实际服务器地址
|
||||
timeout: 30000,
|
||||
autoReconnect: true,
|
||||
reconnectInterval: 3000,
|
||||
maxReconnectTimes: 5
|
||||
};
|
||||
|
||||
// 5. 初始化
|
||||
netManager.init(config);
|
||||
|
||||
// 6. 连接服务器 (HttpClient 创建即可用)
|
||||
const success = await netManager.connect();
|
||||
|
||||
if (success) {
|
||||
console.log('✅ 网络初始化成功');
|
||||
} else {
|
||||
console.error('❌ 网络初始化失败');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接成功后的处理
|
||||
*/
|
||||
private async onConnected() {
|
||||
const netManager = NetManager.getInstance();
|
||||
|
||||
// 示例: 调用登录 API
|
||||
// 需要先同步服务端协议并导入类型定义
|
||||
// import { ReqLogin, ResLogin } from '../../Shared/protocols/PtlLogin';
|
||||
//
|
||||
// const result = await netManager.callApi<ReqLogin, ResLogin>('login', {
|
||||
// username: 'testUser',
|
||||
// password: '123456'
|
||||
// });
|
||||
//
|
||||
// if (result) {
|
||||
// console.log('登录成功:', result);
|
||||
// }
|
||||
|
||||
// 示例: 监听服务器消息
|
||||
// import { MsgUserJoin } from '../../Shared/protocols/MsgUserJoin';
|
||||
//
|
||||
// netManager.listenMsg('UserJoin', (msg: MsgUserJoin) => {
|
||||
// console.log('有新用户加入:', msg);
|
||||
// });
|
||||
|
||||
console.log('');
|
||||
console.log('========================================');
|
||||
console.log('📝 使用步骤:');
|
||||
console.log('1. 运行 npm run sync-shared 同步协议');
|
||||
console.log('2. 从 Shared 导入 serviceProto');
|
||||
console.log('3. 调用 setServiceProto(serviceProto)');
|
||||
console.log('4. 就可以使用 callApi 和 listenMsg 了');
|
||||
console.log('========================================');
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
// 断开网络连接
|
||||
NetManager.getInstance().disconnect();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user