feat: 移除Socket.IO依赖,实现原生WebSocket支持

- 移除所有Socket.IO相关装饰器和依赖
- 创建CleanWebSocketGateway使用原生WebSocket Server
- 实现完整的多客户端实时同步功能
- 支持地图房间分组管理
- 支持本地和全局消息广播
- 支持位置更新实时同步
- 更新API文档和连接信息
- 完成多客户端同步测试验证

技术改进:
- 使用原生ws库替代Socket.IO,减少依赖
- 实现更高效的消息路由和广播机制
- 添加地图房间自动管理功能
- 提供实时连接统计和监控接口

测试验证:
-  多客户端连接和认证
-  聊天消息实时同步
-  位置更新广播
-  地图房间分组
-  系统状态监控
This commit is contained in:
moyin
2026-01-09 17:00:23 +08:00
parent ece4e6f5a2
commit e9dc887c59
4 changed files with 563 additions and 112 deletions

View File

@@ -43,6 +43,7 @@ import {
import { JwtAuthGuard } from '../auth/jwt_auth.guard';
import { ZulipService } from './zulip.service';
import { ZulipWebSocketGateway } from './zulip_websocket.gateway';
import { CleanWebSocketGateway } from './clean_websocket.gateway';
import {
SendChatMessageDto,
ChatMessageResponseDto,
@@ -58,7 +59,7 @@ export class ChatController {
constructor(
private readonly zulipService: ZulipService,
private readonly websocketGateway: ZulipWebSocketGateway,
private readonly websocketGateway: CleanWebSocketGateway,
) {}
/**
@@ -255,6 +256,7 @@ export class ChatController {
// 获取 WebSocket 连接状态
const totalConnections = await this.websocketGateway.getConnectionCount();
const authenticatedConnections = await this.websocketGateway.getAuthenticatedConnectionCount();
const mapPlayerCounts = await this.websocketGateway.getMapPlayerCounts();
// 获取内存使用情况
const memoryUsage = process.memoryUsage();
@@ -267,11 +269,7 @@ export class ChatController {
totalConnections,
authenticatedConnections,
activeSessions: authenticatedConnections, // 简化处理
mapPlayerCounts: {
'whale_port': Math.floor(authenticatedConnections * 0.4),
'pumpkin_valley': Math.floor(authenticatedConnections * 0.3),
'novice_village': Math.floor(authenticatedConnections * 0.3),
},
mapPlayerCounts: mapPlayerCounts,
},
zulip: {
serverConnected: true, // 需要实际检查
@@ -349,19 +347,21 @@ export class ChatController {
})
async getWebSocketInfo() {
return {
websocketUrl: 'ws://localhost:3000/game',
namespace: '/game',
websocketUrl: 'ws://localhost:3001',
namespace: '/',
supportedEvents: [
'login', // 用户登录
'chat', // 发送聊天消息
'position_update', // 位置更新
'position', // 位置更新
],
supportedResponses: [
'connected', // 连接确认
'login_success', // 登录成功
'login_error', // 登录失败
'chat_sent', // 消息发送成功
'chat_error', // 消息发送失败
'chat_render', // 接收到聊天消息
'error', // 通用错误
],
authRequired: true,
tokenType: 'JWT',