更新network库及core库优化
This commit is contained in:
445
packages/network-client/src/transport/ClientTransport.ts
Normal file
445
packages/network-client/src/transport/ClientTransport.ts
Normal file
@@ -0,0 +1,445 @@
|
||||
/**
|
||||
* 客户端传输层抽象接口
|
||||
*/
|
||||
|
||||
import { Emitter, ITimer, Core } from '@esengine/ecs-framework';
|
||||
import { NetworkValue } from '@esengine/ecs-framework-network-shared';
|
||||
|
||||
/**
|
||||
* 客户端传输配置
|
||||
*/
|
||||
export interface ClientTransportConfig {
|
||||
/** 服务器地址 */
|
||||
host: string;
|
||||
/** 服务器端口 */
|
||||
port: number;
|
||||
/** 是否使用安全连接 */
|
||||
secure?: boolean;
|
||||
/** 连接超时时间(毫秒) */
|
||||
connectionTimeout?: number;
|
||||
/** 重连间隔(毫秒) */
|
||||
reconnectInterval?: number;
|
||||
/** 最大重连次数 */
|
||||
maxReconnectAttempts?: number;
|
||||
/** 心跳间隔(毫秒) */
|
||||
heartbeatInterval?: number;
|
||||
/** 消息队列最大大小 */
|
||||
maxQueueSize?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接状态
|
||||
*/
|
||||
export enum ConnectionState {
|
||||
/** 断开连接 */
|
||||
DISCONNECTED = 'disconnected',
|
||||
/** 连接中 */
|
||||
CONNECTING = 'connecting',
|
||||
/** 已连接 */
|
||||
CONNECTED = 'connected',
|
||||
/** 认证中 */
|
||||
AUTHENTICATING = 'authenticating',
|
||||
/** 已认证 */
|
||||
AUTHENTICATED = 'authenticated',
|
||||
/** 重连中 */
|
||||
RECONNECTING = 'reconnecting',
|
||||
/** 连接错误 */
|
||||
ERROR = 'error'
|
||||
}
|
||||
|
||||
/**
|
||||
* 客户端消息
|
||||
*/
|
||||
export interface ClientMessage {
|
||||
/** 消息类型 */
|
||||
type: 'rpc' | 'syncvar' | 'system' | 'custom';
|
||||
/** 消息数据 */
|
||||
data: NetworkValue;
|
||||
/** 消息ID(用于响应匹配) */
|
||||
messageId?: string;
|
||||
/** 是否可靠传输 */
|
||||
reliable?: boolean;
|
||||
/** 时间戳 */
|
||||
timestamp?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接统计信息
|
||||
*/
|
||||
export interface ConnectionStats {
|
||||
/** 连接时间 */
|
||||
connectedAt: Date | null;
|
||||
/** 连接持续时间(毫秒) */
|
||||
connectionDuration: number;
|
||||
/** 发送消息数 */
|
||||
messagesSent: number;
|
||||
/** 接收消息数 */
|
||||
messagesReceived: number;
|
||||
/** 发送字节数 */
|
||||
bytesSent: number;
|
||||
/** 接收字节数 */
|
||||
bytesReceived: number;
|
||||
/** 重连次数 */
|
||||
reconnectCount: number;
|
||||
/** 丢失消息数 */
|
||||
messagesLost: number;
|
||||
/** 平均延迟(毫秒) */
|
||||
averageLatency: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 客户端传输事件
|
||||
*/
|
||||
export interface ClientTransportEvents {
|
||||
/** 连接建立 */
|
||||
'connected': () => void;
|
||||
/** 连接断开 */
|
||||
'disconnected': (reason: string) => void;
|
||||
/** 连接状态变化 */
|
||||
'state-changed': (oldState: ConnectionState, newState: ConnectionState) => void;
|
||||
/** 收到消息 */
|
||||
'message': (message: ClientMessage) => void;
|
||||
/** 连接错误 */
|
||||
'error': (error: Error) => void;
|
||||
/** 重连开始 */
|
||||
'reconnecting': (attempt: number, maxAttempts: number) => void;
|
||||
/** 重连成功 */
|
||||
'reconnected': () => void;
|
||||
/** 重连失败 */
|
||||
'reconnect-failed': () => void;
|
||||
/** 延迟更新 */
|
||||
'latency-updated': (latency: number) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 客户端传输层抽象类
|
||||
*/
|
||||
export abstract class ClientTransport {
|
||||
protected config: ClientTransportConfig;
|
||||
protected state: ConnectionState = ConnectionState.DISCONNECTED;
|
||||
protected stats: ConnectionStats;
|
||||
protected messageQueue: ClientMessage[] = [];
|
||||
protected reconnectAttempts = 0;
|
||||
protected reconnectTimer: ITimer<any> | null = null;
|
||||
protected heartbeatTimer: ITimer<any> | null = null;
|
||||
private latencyMeasurements: number[] = [];
|
||||
private eventEmitter: Emitter<keyof ClientTransportEvents, any>;
|
||||
|
||||
constructor(config: ClientTransportConfig) {
|
||||
this.eventEmitter = new Emitter<keyof ClientTransportEvents, any>();
|
||||
|
||||
this.config = {
|
||||
secure: false,
|
||||
connectionTimeout: 10000, // 10秒
|
||||
reconnectInterval: 3000, // 3秒
|
||||
maxReconnectAttempts: 10,
|
||||
heartbeatInterval: 30000, // 30秒
|
||||
maxQueueSize: 1000,
|
||||
...config
|
||||
};
|
||||
|
||||
this.stats = {
|
||||
connectedAt: null,
|
||||
connectionDuration: 0,
|
||||
messagesSent: 0,
|
||||
messagesReceived: 0,
|
||||
bytesSent: 0,
|
||||
bytesReceived: 0,
|
||||
reconnectCount: 0,
|
||||
messagesLost: 0,
|
||||
averageLatency: 0
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接到服务器
|
||||
*/
|
||||
abstract connect(): Promise<void>;
|
||||
|
||||
/**
|
||||
* 断开连接
|
||||
*/
|
||||
abstract disconnect(): Promise<void>;
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
*/
|
||||
abstract sendMessage(message: ClientMessage): Promise<boolean>;
|
||||
|
||||
/**
|
||||
* 获取当前连接状态
|
||||
*/
|
||||
getState(): ConnectionState {
|
||||
return this.state;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否已连接
|
||||
*/
|
||||
isConnected(): boolean {
|
||||
return this.state === ConnectionState.CONNECTED ||
|
||||
this.state === ConnectionState.AUTHENTICATED;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取连接统计信息
|
||||
*/
|
||||
getStats(): ConnectionStats {
|
||||
if (this.stats.connectedAt) {
|
||||
this.stats.connectionDuration = Date.now() - this.stats.connectedAt.getTime();
|
||||
}
|
||||
return { ...this.stats };
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置
|
||||
*/
|
||||
getConfig(): Readonly<ClientTransportConfig> {
|
||||
return this.config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置状态
|
||||
*/
|
||||
protected setState(newState: ConnectionState): void {
|
||||
if (this.state !== newState) {
|
||||
const oldState = this.state;
|
||||
this.state = newState;
|
||||
this.eventEmitter.emit('state-changed', oldState, newState);
|
||||
|
||||
// 特殊状态处理
|
||||
if (newState === ConnectionState.CONNECTED) {
|
||||
this.stats.connectedAt = new Date();
|
||||
this.reconnectAttempts = 0;
|
||||
this.startHeartbeat();
|
||||
this.processMessageQueue();
|
||||
this.eventEmitter.emit('connected');
|
||||
|
||||
if (oldState === ConnectionState.RECONNECTING) {
|
||||
this.eventEmitter.emit('reconnected');
|
||||
}
|
||||
} else if (newState === ConnectionState.DISCONNECTED) {
|
||||
this.stats.connectedAt = null;
|
||||
this.stopHeartbeat();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理接收到的消息
|
||||
*/
|
||||
protected handleMessage(message: ClientMessage): void {
|
||||
this.stats.messagesReceived++;
|
||||
|
||||
if (message.data) {
|
||||
try {
|
||||
const messageSize = JSON.stringify(message.data).length;
|
||||
this.stats.bytesReceived += messageSize;
|
||||
} catch (error) {
|
||||
// 忽略序列化错误
|
||||
}
|
||||
}
|
||||
|
||||
// 处理系统消息
|
||||
if (message.type === 'system') {
|
||||
this.handleSystemMessage(message);
|
||||
return;
|
||||
}
|
||||
|
||||
this.eventEmitter.emit('message', message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理系统消息
|
||||
*/
|
||||
protected handleSystemMessage(message: ClientMessage): void {
|
||||
const data = message.data as any;
|
||||
|
||||
switch (data.action) {
|
||||
case 'ping':
|
||||
// 响应ping
|
||||
this.sendMessage({
|
||||
type: 'system',
|
||||
data: { action: 'pong', timestamp: data.timestamp }
|
||||
});
|
||||
break;
|
||||
|
||||
case 'pong':
|
||||
// 计算延迟
|
||||
if (data.timestamp) {
|
||||
const latency = Date.now() - data.timestamp;
|
||||
this.updateLatency(latency);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理连接错误
|
||||
*/
|
||||
protected handleError(error: Error): void {
|
||||
console.error('Transport error:', error.message);
|
||||
this.eventEmitter.emit('error', error);
|
||||
|
||||
if (this.isConnected()) {
|
||||
this.setState(ConnectionState.ERROR);
|
||||
this.startReconnect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始重连
|
||||
*/
|
||||
protected startReconnect(): void {
|
||||
if (this.reconnectAttempts >= this.config.maxReconnectAttempts!) {
|
||||
this.eventEmitter.emit('reconnect-failed');
|
||||
return;
|
||||
}
|
||||
|
||||
this.setState(ConnectionState.RECONNECTING);
|
||||
this.reconnectAttempts++;
|
||||
this.stats.reconnectCount++;
|
||||
|
||||
this.eventEmitter.emit('reconnecting', this.reconnectAttempts, this.config.maxReconnectAttempts!);
|
||||
|
||||
this.reconnectTimer = Core.schedule(this.config.reconnectInterval! / 1000, false, this, async () => {
|
||||
try {
|
||||
await this.connect();
|
||||
} catch (error) {
|
||||
this.startReconnect(); // 继续重连
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止重连
|
||||
*/
|
||||
protected stopReconnect(): void {
|
||||
if (this.reconnectTimer) {
|
||||
this.reconnectTimer.stop();
|
||||
this.reconnectTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将消息加入队列
|
||||
*/
|
||||
protected queueMessage(message: ClientMessage): boolean {
|
||||
if (this.messageQueue.length >= this.config.maxQueueSize!) {
|
||||
this.stats.messagesLost++;
|
||||
return false;
|
||||
}
|
||||
|
||||
this.messageQueue.push(message);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理消息队列
|
||||
*/
|
||||
protected async processMessageQueue(): Promise<void> {
|
||||
while (this.messageQueue.length > 0 && this.isConnected()) {
|
||||
const message = this.messageQueue.shift()!;
|
||||
await this.sendMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始心跳
|
||||
*/
|
||||
protected startHeartbeat(): void {
|
||||
if (this.config.heartbeatInterval && this.config.heartbeatInterval > 0) {
|
||||
this.heartbeatTimer = Core.schedule(this.config.heartbeatInterval / 1000, true, this, () => {
|
||||
this.sendHeartbeat();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止心跳
|
||||
*/
|
||||
protected stopHeartbeat(): void {
|
||||
if (this.heartbeatTimer) {
|
||||
this.heartbeatTimer.stop();
|
||||
this.heartbeatTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送心跳
|
||||
*/
|
||||
protected sendHeartbeat(): void {
|
||||
this.sendMessage({
|
||||
type: 'system',
|
||||
data: { action: 'ping', timestamp: Date.now() }
|
||||
}).catch(() => {
|
||||
// 心跳发送失败,可能连接有问题
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新延迟统计
|
||||
*/
|
||||
protected updateLatency(latency: number): void {
|
||||
this.latencyMeasurements.push(latency);
|
||||
|
||||
// 只保留最近的10个测量值
|
||||
if (this.latencyMeasurements.length > 10) {
|
||||
this.latencyMeasurements.shift();
|
||||
}
|
||||
|
||||
// 计算平均延迟
|
||||
const sum = this.latencyMeasurements.reduce((a, b) => a + b, 0);
|
||||
this.stats.averageLatency = sum / this.latencyMeasurements.length;
|
||||
|
||||
this.eventEmitter.emit('latency-updated', latency);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新发送统计
|
||||
*/
|
||||
protected updateSendStats(message: ClientMessage): void {
|
||||
this.stats.messagesSent++;
|
||||
|
||||
if (message.data) {
|
||||
try {
|
||||
const messageSize = JSON.stringify(message.data).length;
|
||||
this.stats.bytesSent += messageSize;
|
||||
} catch (error) {
|
||||
// 忽略序列化错误
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁传输层
|
||||
*/
|
||||
destroy(): void {
|
||||
this.stopReconnect();
|
||||
this.stopHeartbeat();
|
||||
this.messageQueue = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 类型安全的事件监听
|
||||
*/
|
||||
on<K extends keyof ClientTransportEvents>(event: K, listener: ClientTransportEvents[K]): this {
|
||||
this.eventEmitter.addObserver(event, listener, this);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除事件监听
|
||||
*/
|
||||
off<K extends keyof ClientTransportEvents>(event: K, listener: ClientTransportEvents[K]): this {
|
||||
this.eventEmitter.removeObserver(event, listener);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 类型安全的事件触发
|
||||
*/
|
||||
emit<K extends keyof ClientTransportEvents>(event: K, ...args: Parameters<ClientTransportEvents[K]>): void {
|
||||
this.eventEmitter.emit(event, ...args);
|
||||
}
|
||||
}
|
||||
427
packages/network-client/src/transport/HttpClientTransport.ts
Normal file
427
packages/network-client/src/transport/HttpClientTransport.ts
Normal file
@@ -0,0 +1,427 @@
|
||||
/**
|
||||
* HTTP 客户端传输实现
|
||||
*
|
||||
* 支持 REST API 和长轮询
|
||||
*/
|
||||
|
||||
import { Core, ITimer } from '@esengine/ecs-framework';
|
||||
import {
|
||||
ClientTransport,
|
||||
ClientTransportConfig,
|
||||
ConnectionState,
|
||||
ClientMessage
|
||||
} from './ClientTransport';
|
||||
|
||||
/**
|
||||
* HTTP 客户端配置
|
||||
*/
|
||||
export interface HttpClientConfig extends ClientTransportConfig {
|
||||
/** API 路径前缀 */
|
||||
apiPrefix?: string;
|
||||
/** 请求超时时间(毫秒) */
|
||||
requestTimeout?: number;
|
||||
/** 长轮询超时时间(毫秒) */
|
||||
longPollTimeout?: number;
|
||||
/** 是否启用长轮询 */
|
||||
enableLongPolling?: boolean;
|
||||
/** 额外的请求头 */
|
||||
headers?: Record<string, string>;
|
||||
/** 认证令牌 */
|
||||
authToken?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP 响应接口
|
||||
*/
|
||||
interface HttpResponse {
|
||||
success: boolean;
|
||||
data?: any;
|
||||
error?: string;
|
||||
messages?: ClientMessage[];
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP 客户端传输
|
||||
*/
|
||||
export class HttpClientTransport extends ClientTransport {
|
||||
private connectionId: string | null = null;
|
||||
private longPollController: AbortController | null = null;
|
||||
private longPollRunning = false;
|
||||
private connectPromise: Promise<void> | null = null;
|
||||
private requestTimers: Set<ITimer<any>> = new Set();
|
||||
|
||||
protected override config: HttpClientConfig;
|
||||
|
||||
constructor(config: HttpClientConfig) {
|
||||
super(config);
|
||||
|
||||
this.config = {
|
||||
apiPrefix: '/api',
|
||||
requestTimeout: 30000, // 30秒
|
||||
longPollTimeout: 25000, // 25秒
|
||||
enableLongPolling: true,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
...config
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接到服务器
|
||||
*/
|
||||
async connect(): Promise<void> {
|
||||
if (this.state === ConnectionState.CONNECTING ||
|
||||
this.state === ConnectionState.CONNECTED) {
|
||||
return this.connectPromise || Promise.resolve();
|
||||
}
|
||||
|
||||
this.setState(ConnectionState.CONNECTING);
|
||||
this.stopReconnect();
|
||||
|
||||
this.connectPromise = this.performConnect();
|
||||
return this.connectPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行连接
|
||||
*/
|
||||
private async performConnect(): Promise<void> {
|
||||
try {
|
||||
// 发送连接请求
|
||||
const response = await this.makeRequest('/connect', 'POST', {});
|
||||
|
||||
if (response.success && response.data.connectionId) {
|
||||
this.connectionId = response.data.connectionId;
|
||||
this.setState(ConnectionState.CONNECTED);
|
||||
|
||||
// 启动长轮询
|
||||
if (this.config.enableLongPolling) {
|
||||
this.startLongPolling();
|
||||
}
|
||||
} else {
|
||||
throw new Error(response.error || 'Connection failed');
|
||||
}
|
||||
} catch (error) {
|
||||
this.setState(ConnectionState.ERROR);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 断开连接
|
||||
*/
|
||||
async disconnect(): Promise<void> {
|
||||
this.stopReconnect();
|
||||
this.stopLongPolling();
|
||||
|
||||
if (this.connectionId) {
|
||||
try {
|
||||
await this.makeRequest('/disconnect', 'POST', {
|
||||
connectionId: this.connectionId
|
||||
});
|
||||
} catch (error) {
|
||||
// 忽略断开连接时的错误
|
||||
}
|
||||
|
||||
this.connectionId = null;
|
||||
}
|
||||
|
||||
this.setState(ConnectionState.DISCONNECTED);
|
||||
this.connectPromise = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
*/
|
||||
async sendMessage(message: ClientMessage): Promise<boolean> {
|
||||
if (!this.connectionId) {
|
||||
// 如果未连接,将消息加入队列
|
||||
if (this.state === ConnectionState.CONNECTING ||
|
||||
this.state === ConnectionState.RECONNECTING) {
|
||||
return this.queueMessage(message);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await this.makeRequest('/send', 'POST', {
|
||||
connectionId: this.connectionId,
|
||||
message: {
|
||||
...message,
|
||||
timestamp: message.timestamp || Date.now()
|
||||
}
|
||||
});
|
||||
|
||||
if (response.success) {
|
||||
this.updateSendStats(message);
|
||||
return true;
|
||||
} else {
|
||||
console.error('Send message failed:', response.error);
|
||||
return false;
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
this.handleError(error as Error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动长轮询
|
||||
*/
|
||||
private startLongPolling(): void {
|
||||
if (this.longPollRunning || !this.connectionId) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.longPollRunning = true;
|
||||
this.performLongPoll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止长轮询
|
||||
*/
|
||||
private stopLongPolling(): void {
|
||||
this.longPollRunning = false;
|
||||
|
||||
if (this.longPollController) {
|
||||
this.longPollController.abort();
|
||||
this.longPollController = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行长轮询
|
||||
*/
|
||||
private async performLongPoll(): Promise<void> {
|
||||
while (this.longPollRunning && this.connectionId) {
|
||||
try {
|
||||
this.longPollController = new AbortController();
|
||||
|
||||
const response = await this.makeRequest('/poll', 'GET', {
|
||||
connectionId: this.connectionId
|
||||
}, {
|
||||
signal: this.longPollController.signal,
|
||||
timeout: this.config.longPollTimeout
|
||||
});
|
||||
|
||||
if (response.success && response.messages && response.messages.length > 0) {
|
||||
// 处理接收到的消息
|
||||
for (const message of response.messages) {
|
||||
this.handleMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果服务器指示断开连接
|
||||
if (response.data && response.data.disconnected) {
|
||||
this.handleServerDisconnect();
|
||||
break;
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
if ((error as any).name === 'AbortError') {
|
||||
// 被主动取消,正常情况
|
||||
break;
|
||||
}
|
||||
|
||||
console.warn('Long polling error:', (error as Error).message);
|
||||
|
||||
// 如果是网络错误,尝试重连
|
||||
if (this.isNetworkError(error as Error)) {
|
||||
this.handleError(error as Error);
|
||||
break;
|
||||
}
|
||||
|
||||
// 短暂等待后重试
|
||||
await this.delay(1000);
|
||||
}
|
||||
|
||||
this.longPollController = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理服务器主动断开连接
|
||||
*/
|
||||
private handleServerDisconnect(): void {
|
||||
this.connectionId = null;
|
||||
this.stopLongPolling();
|
||||
this.emit('disconnected', 'Server disconnect');
|
||||
|
||||
if (this.reconnectAttempts < this.config.maxReconnectAttempts!) {
|
||||
this.startReconnect();
|
||||
} else {
|
||||
this.setState(ConnectionState.DISCONNECTED);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送 HTTP 请求
|
||||
*/
|
||||
private async makeRequest(
|
||||
path: string,
|
||||
method: 'GET' | 'POST' | 'PUT' | 'DELETE' = 'GET',
|
||||
data?: any,
|
||||
options: {
|
||||
signal?: AbortSignal;
|
||||
timeout?: number;
|
||||
} = {}
|
||||
): Promise<HttpResponse> {
|
||||
const url = this.buildUrl(path);
|
||||
const headers = this.buildHeaders();
|
||||
|
||||
const requestOptions: RequestInit = {
|
||||
method,
|
||||
headers,
|
||||
signal: options.signal
|
||||
};
|
||||
|
||||
// 添加请求体
|
||||
if (method !== 'GET' && data) {
|
||||
requestOptions.body = JSON.stringify(data);
|
||||
} else if (method === 'GET' && data) {
|
||||
// GET 请求将数据作为查询参数
|
||||
const params = new URLSearchParams();
|
||||
Object.entries(data).forEach(([key, value]) => {
|
||||
params.append(key, String(value));
|
||||
});
|
||||
const separator = url.includes('?') ? '&' : '?';
|
||||
return this.fetchWithTimeout(`${url}${separator}${params}`, requestOptions, options.timeout);
|
||||
}
|
||||
|
||||
return this.fetchWithTimeout(url, requestOptions, options.timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* 带超时的 fetch 请求
|
||||
*/
|
||||
private async fetchWithTimeout(
|
||||
url: string,
|
||||
options: RequestInit,
|
||||
timeout?: number
|
||||
): Promise<HttpResponse> {
|
||||
const actualTimeout = timeout || this.config.requestTimeout!;
|
||||
|
||||
const controller = new AbortController();
|
||||
let timeoutTimer: ITimer<any> | null = null;
|
||||
|
||||
// 创建超时定时器
|
||||
timeoutTimer = Core.schedule(actualTimeout / 1000, false, this, () => {
|
||||
controller.abort();
|
||||
if (timeoutTimer) {
|
||||
this.requestTimers.delete(timeoutTimer);
|
||||
}
|
||||
});
|
||||
|
||||
this.requestTimers.add(timeoutTimer);
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
...options,
|
||||
signal: options.signal || controller.signal
|
||||
});
|
||||
|
||||
// 清理定时器
|
||||
if (timeoutTimer) {
|
||||
timeoutTimer.stop();
|
||||
this.requestTimers.delete(timeoutTimer);
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
return result as HttpResponse;
|
||||
|
||||
} catch (error) {
|
||||
// 清理定时器
|
||||
if (timeoutTimer) {
|
||||
timeoutTimer.stop();
|
||||
this.requestTimers.delete(timeoutTimer);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建请求URL
|
||||
*/
|
||||
private buildUrl(path: string): string {
|
||||
const protocol = this.config.secure ? 'https' : 'http';
|
||||
const basePath = this.config.apiPrefix || '';
|
||||
const cleanPath = path.startsWith('/') ? path : `/${path}`;
|
||||
|
||||
return `${protocol}://${this.config.host}:${this.config.port}${basePath}${cleanPath}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建请求头
|
||||
*/
|
||||
private buildHeaders(): Record<string, string> {
|
||||
const headers = { ...this.config.headers };
|
||||
|
||||
if (this.config.authToken) {
|
||||
headers['Authorization'] = `Bearer ${this.config.authToken}`;
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否为网络错误
|
||||
*/
|
||||
private isNetworkError(error: Error): boolean {
|
||||
return error.message.includes('fetch') ||
|
||||
error.message.includes('network') ||
|
||||
error.message.includes('timeout') ||
|
||||
error.name === 'TypeError';
|
||||
}
|
||||
|
||||
/**
|
||||
* 延迟函数
|
||||
*/
|
||||
private delay(ms: number): Promise<void> {
|
||||
return new Promise(resolve => {
|
||||
const timer = Core.schedule(ms / 1000, false, this, () => {
|
||||
this.requestTimers.delete(timer);
|
||||
resolve();
|
||||
});
|
||||
this.requestTimers.add(timer);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置认证令牌
|
||||
*/
|
||||
setAuthToken(token: string): void {
|
||||
this.config.authToken = token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取连接ID
|
||||
*/
|
||||
getConnectionId(): string | null {
|
||||
return this.connectionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否支持 Fetch API
|
||||
*/
|
||||
static isSupported(): boolean {
|
||||
return typeof fetch !== 'undefined';
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁传输层
|
||||
*/
|
||||
override destroy(): void {
|
||||
// 清理所有请求定时器
|
||||
this.requestTimers.forEach(timer => timer.stop());
|
||||
this.requestTimers.clear();
|
||||
|
||||
this.disconnect();
|
||||
super.destroy();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
/**
|
||||
* WebSocket 客户端传输实现
|
||||
*/
|
||||
|
||||
import { Core, ITimer } from '@esengine/ecs-framework';
|
||||
import {
|
||||
ClientTransport,
|
||||
ClientTransportConfig,
|
||||
ConnectionState,
|
||||
ClientMessage
|
||||
} from './ClientTransport';
|
||||
|
||||
/**
|
||||
* WebSocket 客户端配置
|
||||
*/
|
||||
export interface WebSocketClientConfig extends ClientTransportConfig {
|
||||
/** WebSocket 路径 */
|
||||
path?: string;
|
||||
/** 协议列表 */
|
||||
protocols?: string | string[];
|
||||
/** 额外的请求头 */
|
||||
headers?: Record<string, string>;
|
||||
/** 是否启用二进制消息 */
|
||||
binaryType?: 'blob' | 'arraybuffer';
|
||||
/** WebSocket 扩展 */
|
||||
extensions?: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* WebSocket 客户端传输
|
||||
*/
|
||||
export class WebSocketClientTransport extends ClientTransport {
|
||||
private websocket: WebSocket | null = null;
|
||||
private connectionPromise: Promise<void> | null = null;
|
||||
private connectionTimeoutTimer: ITimer<any> | null = null;
|
||||
|
||||
protected override config: WebSocketClientConfig;
|
||||
|
||||
constructor(config: WebSocketClientConfig) {
|
||||
super(config);
|
||||
|
||||
this.config = {
|
||||
path: '/ws',
|
||||
protocols: [],
|
||||
headers: {},
|
||||
binaryType: 'arraybuffer',
|
||||
...config
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接到服务器
|
||||
*/
|
||||
async connect(): Promise<void> {
|
||||
if (this.state === ConnectionState.CONNECTING ||
|
||||
this.state === ConnectionState.CONNECTED) {
|
||||
return this.connectionPromise || Promise.resolve();
|
||||
}
|
||||
|
||||
this.setState(ConnectionState.CONNECTING);
|
||||
this.stopReconnect(); // 停止任何正在进行的重连
|
||||
|
||||
this.connectionPromise = new Promise((resolve, reject) => {
|
||||
try {
|
||||
// 构建WebSocket URL
|
||||
const protocol = this.config.secure ? 'wss' : 'ws';
|
||||
const url = `${protocol}://${this.config.host}:${this.config.port}${this.config.path}`;
|
||||
|
||||
// 创建WebSocket连接
|
||||
this.websocket = new WebSocket(url, this.config.protocols);
|
||||
|
||||
if (this.config.binaryType) {
|
||||
this.websocket.binaryType = this.config.binaryType;
|
||||
}
|
||||
|
||||
// 设置连接超时
|
||||
this.connectionTimeoutTimer = Core.schedule(this.config.connectionTimeout! / 1000, false, this, () => {
|
||||
if (this.websocket && this.websocket.readyState === WebSocket.CONNECTING) {
|
||||
this.websocket.close();
|
||||
reject(new Error('Connection timeout'));
|
||||
}
|
||||
});
|
||||
|
||||
// WebSocket 事件处理
|
||||
this.websocket.onopen = () => {
|
||||
if (this.connectionTimeoutTimer) {
|
||||
this.connectionTimeoutTimer.stop();
|
||||
this.connectionTimeoutTimer = null;
|
||||
}
|
||||
this.setState(ConnectionState.CONNECTED);
|
||||
resolve();
|
||||
};
|
||||
|
||||
this.websocket.onclose = (event) => {
|
||||
if (this.connectionTimeoutTimer) {
|
||||
this.connectionTimeoutTimer.stop();
|
||||
this.connectionTimeoutTimer = null;
|
||||
}
|
||||
this.handleClose(event.code, event.reason);
|
||||
|
||||
if (this.state === ConnectionState.CONNECTING) {
|
||||
reject(new Error(`Connection failed: ${event.reason || 'Unknown error'}`));
|
||||
}
|
||||
};
|
||||
|
||||
this.websocket.onerror = (event) => {
|
||||
if (this.connectionTimeoutTimer) {
|
||||
this.connectionTimeoutTimer.stop();
|
||||
this.connectionTimeoutTimer = null;
|
||||
}
|
||||
const error = new Error('WebSocket error');
|
||||
this.handleError(error);
|
||||
|
||||
if (this.state === ConnectionState.CONNECTING) {
|
||||
reject(error);
|
||||
}
|
||||
};
|
||||
|
||||
this.websocket.onmessage = (event) => {
|
||||
this.handleWebSocketMessage(event);
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
this.setState(ConnectionState.ERROR);
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
|
||||
return this.connectionPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
* 断开连接
|
||||
*/
|
||||
async disconnect(): Promise<void> {
|
||||
this.stopReconnect();
|
||||
|
||||
if (this.websocket) {
|
||||
// 设置状态为断开连接,避免触发重连
|
||||
this.setState(ConnectionState.DISCONNECTED);
|
||||
|
||||
if (this.websocket.readyState === WebSocket.OPEN ||
|
||||
this.websocket.readyState === WebSocket.CONNECTING) {
|
||||
this.websocket.close(1000, 'Client disconnect');
|
||||
}
|
||||
|
||||
this.websocket = null;
|
||||
}
|
||||
|
||||
this.connectionPromise = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
*/
|
||||
async sendMessage(message: ClientMessage): Promise<boolean> {
|
||||
if (!this.websocket || this.websocket.readyState !== WebSocket.OPEN) {
|
||||
// 如果未连接,将消息加入队列
|
||||
if (this.state === ConnectionState.CONNECTING ||
|
||||
this.state === ConnectionState.RECONNECTING) {
|
||||
return this.queueMessage(message);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
// 序列化消息
|
||||
const serialized = JSON.stringify({
|
||||
...message,
|
||||
timestamp: message.timestamp || Date.now()
|
||||
});
|
||||
|
||||
// 发送消息
|
||||
this.websocket.send(serialized);
|
||||
this.updateSendStats(message);
|
||||
|
||||
return true;
|
||||
|
||||
} catch (error) {
|
||||
this.handleError(error as Error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理 WebSocket 消息
|
||||
*/
|
||||
private handleWebSocketMessage(event: MessageEvent): void {
|
||||
try {
|
||||
let data: string;
|
||||
|
||||
if (event.data instanceof ArrayBuffer) {
|
||||
// 处理二进制数据
|
||||
data = new TextDecoder().decode(event.data);
|
||||
} else if (event.data instanceof Blob) {
|
||||
// Blob 需要异步处理
|
||||
event.data.text().then(text => {
|
||||
this.processMessage(text);
|
||||
});
|
||||
return;
|
||||
} else {
|
||||
// 字符串数据
|
||||
data = event.data;
|
||||
}
|
||||
|
||||
this.processMessage(data);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error processing WebSocket message:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理消息内容
|
||||
*/
|
||||
private processMessage(data: string): void {
|
||||
try {
|
||||
const message: ClientMessage = JSON.parse(data);
|
||||
this.handleMessage(message);
|
||||
} catch (error) {
|
||||
console.error('Error parsing message:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理连接关闭
|
||||
*/
|
||||
private handleClose(code: number, reason: string): void {
|
||||
this.websocket = null;
|
||||
this.connectionPromise = null;
|
||||
|
||||
const wasConnected = this.isConnected();
|
||||
|
||||
// 根据关闭代码决定是否重连
|
||||
if (code === 1000) {
|
||||
// 正常关闭,不重连
|
||||
this.setState(ConnectionState.DISCONNECTED);
|
||||
this.emit('disconnected', reason || 'Normal closure');
|
||||
} else if (wasConnected && this.reconnectAttempts < this.config.maxReconnectAttempts!) {
|
||||
// 异常关闭,尝试重连
|
||||
this.emit('disconnected', reason || `Abnormal closure (${code})`);
|
||||
this.startReconnect();
|
||||
} else {
|
||||
// 达到最大重连次数或其他情况
|
||||
this.setState(ConnectionState.DISCONNECTED);
|
||||
this.emit('disconnected', reason || `Connection lost (${code})`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 WebSocket 就绪状态
|
||||
*/
|
||||
getReadyState(): number {
|
||||
return this.websocket?.readyState ?? WebSocket.CLOSED;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 WebSocket 实例
|
||||
*/
|
||||
getWebSocket(): WebSocket | null {
|
||||
return this.websocket;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否支持 WebSocket
|
||||
*/
|
||||
static isSupported(): boolean {
|
||||
return typeof WebSocket !== 'undefined';
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁传输层
|
||||
*/
|
||||
override destroy(): void {
|
||||
if (this.connectionTimeoutTimer) {
|
||||
this.connectionTimeoutTimer.stop();
|
||||
this.connectionTimeoutTimer = null;
|
||||
}
|
||||
this.disconnect();
|
||||
super.destroy();
|
||||
}
|
||||
}
|
||||
7
packages/network-client/src/transport/index.ts
Normal file
7
packages/network-client/src/transport/index.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* 传输层导出
|
||||
*/
|
||||
|
||||
export * from './ClientTransport';
|
||||
export * from './WebSocketClientTransport';
|
||||
export * from './HttpClientTransport';
|
||||
Reference in New Issue
Block a user