重构NetManager支持MessagePair,新增TSRPCWsClient。

This commit is contained in:
janing
2025-12-18 13:25:04 +08:00
parent c12e439add
commit fb940452db
41 changed files with 976 additions and 681 deletions

View File

@@ -1,7 +1,9 @@
import { sys } from 'cc';
// 使用别名导入避免命名冲突
import { BaseServiceType, HttpClient as HttpClientBrowser } from 'tsrpc-browser';
import { HttpClient as HttpClientMiniapp } from 'tsrpc-miniapp';
import { BaseServiceType, HttpClient as HttpClientBrowser, WsClient as WsClientBrowser } from 'tsrpc-browser';
import { HttpClient as HttpClientMiniapp, WsClient as WsClientMiniapp } from 'tsrpc-miniapp';
import { NetProtocolType } from './NetConfig';
import { INetClient, WebSocketClientWrapper } from './WebSocketClient';
/**
* 平台类型枚举
@@ -9,10 +11,10 @@ import { HttpClient as HttpClientMiniapp } from 'tsrpc-miniapp';
export enum PlatformType {
/** 浏览器平台 */
Browser = "browser",
/** 小程序平台 (微信、抖音、QQ等) */
MiniApp = "miniapp",
/** 未知平台 */
Unknown = "unknown"
}
@@ -23,22 +25,29 @@ export enum PlatformType {
export interface ClientConfig {
/** 服务器地址 */
server: string;
/** 网络协议类型 */
protocolType?: NetProtocolType;
/** 是否使用 JSON 格式 (默认 true) */
json?: boolean;
/** 超时时间(ms) */
timeout?: number;
/** 其他配置 */
[key: string]: any;
}
/**
* 平台适配器
* 根据当前平台返回对应的 TSRPC 客户端实现
* 根据当前平台和协议类型返回对应的 TSRPC 客户端实现
*
* 注意: TSRPC 不同平台的库中 API 是重名的,所以使用别名导入
* 支持协议:
* - HTTP/HTTPS: 用于无状态的API调用
* - WebSocket: 用于实时双向通信
*
* 支持平台:
* - tsrpc-browser: 用于浏览器和 XMLHttpRequest 兼容的环境
* - tsrpc-miniapp: 用于微信、抖音、QQ 等小程序环境
*/
@@ -94,11 +103,12 @@ export class PlatformAdapter {
}
/**
* 创建对应平台的 TSRPC 客户端实例
* 创建对应平台和协议的 TSRPC 客户端实例
* @param config 客户端配置
*/
static createClient<T extends BaseServiceType>(config: ClientConfig): HttpClientBrowser<T> | HttpClientMiniapp<T> {
static createClient<T extends BaseServiceType>(config: ClientConfig): INetClient<T> {
const platform = this.detectPlatform();
const protocolType = config.protocolType || NetProtocolType.Http;
// 默认配置
const defaultConfig: ClientConfig = {
@@ -115,19 +125,50 @@ export class PlatformAdapter {
console.warn('[PlatformAdapter] Service protocol not set, please call setServiceProto() first');
}
console.log(`[PlatformAdapter] Creating ${protocolType} client for ${platform}:`, defaultConfig.server);
// 根据协议类型和平台创建对应的客户端
if (protocolType === NetProtocolType.WebSocket) {
return this.createWebSocketClient<T>(platform, defaultConfig);
} else {
return this.createHttpClient<T>(platform, defaultConfig);
}
}
/**
* 创建 HTTP 客户端
*/
private static createHttpClient<T extends BaseServiceType>(platform: PlatformType, config: ClientConfig): INetClient<T> {
let client: HttpClientBrowser<T> | HttpClientMiniapp<T>;
// 根据平台创建对应的客户端
if (platform === PlatformType.MiniApp) {
console.log('[PlatformAdapter] Creating MiniApp client:', defaultConfig.server);
client = new HttpClientMiniapp(this._serviceProto, defaultConfig) as HttpClientMiniapp<T>;
client = new HttpClientMiniapp(this._serviceProto, config) as HttpClientMiniapp<T>;
} else {
// 浏览器和其他 XMLHttpRequest 兼容环境使用 Browser 客户端
console.log('[PlatformAdapter] Creating Browser client:', defaultConfig.server);
client = new HttpClientBrowser(this._serviceProto, defaultConfig) as HttpClientBrowser<T>;
client = new HttpClientBrowser(this._serviceProto, config) as HttpClientBrowser<T>;
}
return client;
// 包装 HTTP 客户端使其符合 INetClient 接口
return {
callApi: async <Req, Res>(apiName: string, req: Req) => {
return await client.callApi(apiName, req);
}
} as INetClient<T>;
}
/**
* 创建 WebSocket 客户端
*/
private static createWebSocketClient<T extends BaseServiceType>(platform: PlatformType, config: ClientConfig): INetClient<T> {
let wsClient: WsClientBrowser<T> | WsClientMiniapp<T>;
if (platform === PlatformType.MiniApp) {
wsClient = new WsClientMiniapp(this._serviceProto, config) as WsClientMiniapp<T>;
} else {
wsClient = new WsClientBrowser(this._serviceProto, config) as WsClientBrowser<T>;
}
// 使用包装器统一接口
return new WebSocketClientWrapper<T>(wsClient);
}
/**