websocket:增强Zulip WebSocket网关的调试和监控功能
- 添加详细的连接和断开日志记录 - 增强错误处理和异常捕获机制 - 完善客户端状态管理和会话跟踪 - 优化消息处理的调试输出 提升WebSocket连接问题的诊断能力
This commit is contained in:
@@ -139,6 +139,9 @@ export class ZulipWebSocketGateway implements OnGatewayConnection, OnGatewayDisc
|
||||
namespace: '/game',
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
// 设置消息分发器,使ZulipEventProcessorService能够向客户端发送消息
|
||||
this.setupMessageDistributor();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -373,6 +376,13 @@ export class ZulipWebSocketGateway implements OnGatewayConnection, OnGatewayDisc
|
||||
): Promise<void> {
|
||||
const clientData = client.data as ClientData | undefined;
|
||||
|
||||
console.log('🔍 DEBUG: handleChat 被调用了!', {
|
||||
socketId: client.id,
|
||||
data: data,
|
||||
clientData: clientData,
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
this.logger.log('收到聊天消息', {
|
||||
operation: 'handleChat',
|
||||
socketId: client.id,
|
||||
@@ -749,5 +759,41 @@ export class ZulipWebSocketGateway implements OnGatewayConnection, OnGatewayDisc
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置消息分发器
|
||||
*
|
||||
* 功能描述:
|
||||
* 将当前WebSocket网关设置为ZulipEventProcessorService的消息分发器,
|
||||
* 使其能够接收从Zulip返回的消息并转发给游戏客户端
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
private setupMessageDistributor(): void {
|
||||
try {
|
||||
// 获取ZulipEventProcessorService实例
|
||||
const eventProcessor = this.zulipService.getEventProcessor();
|
||||
|
||||
if (eventProcessor) {
|
||||
// 设置消息分发器
|
||||
eventProcessor.setMessageDistributor(this);
|
||||
|
||||
this.logger.log('消息分发器设置完成', {
|
||||
operation: 'setupMessageDistributor',
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
} else {
|
||||
this.logger.warn('无法获取ZulipEventProcessorService实例', {
|
||||
operation: 'setupMessageDistributor',
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
const err = error as Error;
|
||||
this.logger.error('设置消息分发器失败', {
|
||||
operation: 'setupMessageDistributor',
|
||||
error: err.message,
|
||||
}, err.stack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user