forked from datawhale/whale-town-end
refactor: 更新WebSocket相关测试和location_broadcast模块
- 更新location_broadcast网关以支持原生WebSocket - 修改WebSocket认证守卫和中间件 - 更新相关的测试文件和规范 - 添加WebSocket测试工具 - 完善Zulip服务的测试覆盖 技术改进: - 统一WebSocket实现架构 - 优化性能监控和限流中间件 - 更新测试用例以适配新的WebSocket实现
This commit is contained in:
@@ -14,18 +14,18 @@
|
||||
* - 实时广播:向会话中的其他用户广播位置更新
|
||||
*
|
||||
* 技术实现:
|
||||
* - Socket.IO:提供WebSocket通信能力
|
||||
* - 原生WebSocket:提供WebSocket通信能力
|
||||
* - JWT认证:保护需要认证的WebSocket事件
|
||||
* - 核心服务集成:调用位置广播核心服务处理业务逻辑
|
||||
* - 异常处理:统一的WebSocket异常处理和错误响应
|
||||
*
|
||||
* 最近修改:
|
||||
* - 2026-01-08: 代码重构 - 提取魔法数字为常量,优化代码质量 (修改者: moyin)
|
||||
* - 2026-01-09: 重构为原生WebSocket - 移除Socket.IO依赖,使用原生WebSocket (修改者: moyin)
|
||||
*
|
||||
* @author moyin
|
||||
* @version 1.1.0
|
||||
* @version 2.0.0
|
||||
* @since 2026-01-08
|
||||
* @lastModified 2026-01-08
|
||||
* @lastModified 2026-01-09
|
||||
*/
|
||||
|
||||
import {
|
||||
@@ -39,7 +39,8 @@ import {
|
||||
OnGatewayInit,
|
||||
WsException,
|
||||
} from '@nestjs/websockets';
|
||||
import { Server, Socket } from 'socket.io';
|
||||
import { Server } from 'ws';
|
||||
import * as WebSocket from 'ws';
|
||||
import { Logger, UseFilters, UseGuards, UsePipes, ValidationPipe, ArgumentsHost, Inject } from '@nestjs/common';
|
||||
import { BaseWsExceptionFilter } from '@nestjs/websockets';
|
||||
|
||||
@@ -68,6 +69,17 @@ import {
|
||||
// 导入核心服务接口
|
||||
import { Position } from '../../core/location_broadcast_core/position.interface';
|
||||
|
||||
/**
|
||||
* 扩展的WebSocket接口,包含用户信息
|
||||
*/
|
||||
interface ExtendedWebSocket extends WebSocket {
|
||||
id: string;
|
||||
userId?: string;
|
||||
sessionIds?: Set<string>;
|
||||
connectionTimeout?: NodeJS.Timeout;
|
||||
isAlive?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* WebSocket异常过滤器
|
||||
*
|
||||
@@ -80,7 +92,7 @@ class WebSocketExceptionFilter extends BaseWsExceptionFilter {
|
||||
private readonly logger = new Logger(WebSocketExceptionFilter.name);
|
||||
|
||||
catch(exception: any, host: ArgumentsHost) {
|
||||
const client = host.switchToWs().getClient<Socket>();
|
||||
const client = host.switchToWs().getClient<ExtendedWebSocket>();
|
||||
|
||||
const error: ErrorResponse = {
|
||||
type: 'error',
|
||||
@@ -98,7 +110,13 @@ class WebSocketExceptionFilter extends BaseWsExceptionFilter {
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
client.emit('error', error);
|
||||
this.sendMessage(client, 'error', error);
|
||||
}
|
||||
|
||||
private sendMessage(client: ExtendedWebSocket, event: string, data: any) {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
client.send(JSON.stringify({ event, data }));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,8 +126,7 @@ class WebSocketExceptionFilter extends BaseWsExceptionFilter {
|
||||
methods: ['GET', 'POST'],
|
||||
credentials: true,
|
||||
},
|
||||
namespace: '/location-broadcast', // 使用专门的命名空间
|
||||
transports: ['websocket', 'polling'], // 支持WebSocket和轮询
|
||||
path: '/location-broadcast', // WebSocket路径
|
||||
})
|
||||
@UseFilters(new WebSocketExceptionFilter())
|
||||
export class LocationBroadcastGateway
|
||||
@@ -119,11 +136,15 @@ export class LocationBroadcastGateway
|
||||
server: Server;
|
||||
|
||||
private readonly logger = new Logger(LocationBroadcastGateway.name);
|
||||
private clients = new Map<string, ExtendedWebSocket>();
|
||||
private sessionRooms = new Map<string, Set<string>>(); // sessionId -> Set<clientId>
|
||||
|
||||
/** 连接超时时间(分钟) */
|
||||
private static readonly CONNECTION_TIMEOUT_MINUTES = 30;
|
||||
/** 时间转换常量 */
|
||||
private static readonly MILLISECONDS_PER_MINUTE = 60 * 1000;
|
||||
/** 心跳间隔(毫秒) */
|
||||
private static readonly HEARTBEAT_INTERVAL = 30000;
|
||||
|
||||
// 中间件实例
|
||||
private readonly rateLimitMiddleware = new RateLimitMiddleware();
|
||||
@@ -136,51 +157,35 @@ export class LocationBroadcastGateway
|
||||
|
||||
/**
|
||||
* WebSocket服务器初始化
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 配置Socket.IO服务器选项
|
||||
* 2. 设置中间件和事件监听器
|
||||
* 3. 初始化连接池和监控
|
||||
* 4. 记录服务器启动日志
|
||||
*/
|
||||
afterInit(server: Server) {
|
||||
this.logger.log('位置广播WebSocket服务器初始化完成', {
|
||||
namespace: '/location-broadcast',
|
||||
path: '/location-broadcast',
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
// 设置服务器级别的中间件
|
||||
server.use((socket, next) => {
|
||||
this.logger.debug('新的WebSocket连接尝试', {
|
||||
socketId: socket.id,
|
||||
remoteAddress: socket.handshake.address,
|
||||
userAgent: socket.handshake.headers['user-agent'],
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
next();
|
||||
});
|
||||
// 设置心跳检测
|
||||
this.setupHeartbeat();
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理客户端连接
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 记录连接建立日志
|
||||
* 2. 初始化客户端状态
|
||||
* 3. 发送连接确认消息
|
||||
* 4. 设置连接超时和心跳检测
|
||||
*
|
||||
* @param client WebSocket客户端
|
||||
*/
|
||||
handleConnection(client: Socket) {
|
||||
handleConnection(client: ExtendedWebSocket) {
|
||||
// 生成唯一ID
|
||||
client.id = this.generateClientId();
|
||||
client.sessionIds = new Set();
|
||||
client.isAlive = true;
|
||||
|
||||
this.clients.set(client.id, client);
|
||||
|
||||
this.logger.log('WebSocket客户端连接', {
|
||||
socketId: client.id,
|
||||
remoteAddress: client.handshake.address,
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
// 记录连接事件到性能监控
|
||||
this.performanceMonitor.recordConnection(client, true);
|
||||
this.performanceMonitor.recordConnection(client as any, true);
|
||||
|
||||
// 发送连接确认消息
|
||||
const welcomeMessage = {
|
||||
@@ -190,33 +195,34 @@ export class LocationBroadcastGateway
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
client.emit('welcome', welcomeMessage);
|
||||
this.sendMessage(client, 'welcome', welcomeMessage);
|
||||
|
||||
// 设置连接超时(30分钟无活动自动断开)
|
||||
const timeout = setTimeout(() => {
|
||||
this.logger.warn('客户端连接超时,自动断开', {
|
||||
socketId: client.id,
|
||||
timeout: `${LocationBroadcastGateway.CONNECTION_TIMEOUT_MINUTES}分钟`,
|
||||
});
|
||||
client.disconnect(true);
|
||||
}, LocationBroadcastGateway.CONNECTION_TIMEOUT_MINUTES * LocationBroadcastGateway.MILLISECONDS_PER_MINUTE);
|
||||
// 设置连接超时
|
||||
this.setConnectionTimeout(client);
|
||||
|
||||
// 将超时ID存储到客户端对象中
|
||||
(client as any).connectionTimeout = timeout;
|
||||
// 设置消息处理
|
||||
client.on('message', (data) => {
|
||||
try {
|
||||
const message = JSON.parse(data.toString());
|
||||
this.handleMessage(client, message);
|
||||
} catch (error) {
|
||||
this.logger.error('解析消息失败', {
|
||||
socketId: client.id,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 设置pong响应
|
||||
client.on('pong', () => {
|
||||
client.isAlive = true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理客户端断开连接
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 清理客户端相关数据
|
||||
* 2. 从所有会话中移除用户
|
||||
* 3. 通知其他用户该用户离开
|
||||
* 4. 记录断开连接日志
|
||||
*
|
||||
* @param client WebSocket客户端
|
||||
*/
|
||||
async handleDisconnect(client: Socket) {
|
||||
async handleDisconnect(client: ExtendedWebSocket) {
|
||||
const startTime = Date.now();
|
||||
|
||||
this.logger.log('WebSocket客户端断开连接', {
|
||||
@@ -225,25 +231,39 @@ export class LocationBroadcastGateway
|
||||
});
|
||||
|
||||
// 记录断开连接事件到性能监控
|
||||
this.performanceMonitor.recordConnection(client, false);
|
||||
this.performanceMonitor.recordConnection(client as any, false);
|
||||
|
||||
try {
|
||||
// 清理连接超时
|
||||
const timeout = (client as any).connectionTimeout;
|
||||
if (timeout) {
|
||||
clearTimeout(timeout);
|
||||
if (client.connectionTimeout) {
|
||||
clearTimeout(client.connectionTimeout);
|
||||
}
|
||||
|
||||
// 如果是已认证的客户端,进行清理
|
||||
const authenticatedClient = client as AuthenticatedSocket;
|
||||
if (authenticatedClient.userId) {
|
||||
await this.handleUserDisconnection(authenticatedClient, 'connection_lost');
|
||||
if (client.userId) {
|
||||
await this.handleUserDisconnection(client, 'connection_lost');
|
||||
}
|
||||
|
||||
// 从客户端列表中移除
|
||||
this.clients.delete(client.id);
|
||||
|
||||
// 从所有会话房间中移除
|
||||
if (client.sessionIds) {
|
||||
for (const sessionId of client.sessionIds) {
|
||||
const room = this.sessionRooms.get(sessionId);
|
||||
if (room) {
|
||||
room.delete(client.id);
|
||||
if (room.size === 0) {
|
||||
this.sessionRooms.delete(sessionId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const duration = Date.now() - startTime;
|
||||
this.logger.log('客户端断开连接处理完成', {
|
||||
socketId: client.id,
|
||||
userId: authenticatedClient.userId || 'unknown',
|
||||
userId: client.userId || 'unknown',
|
||||
duration,
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
@@ -258,25 +278,36 @@ export class LocationBroadcastGateway
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理加入会话消息
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 验证JWT令牌和用户身份
|
||||
* 2. 将用户添加到指定会话
|
||||
* 3. 获取会话中其他用户的位置信息
|
||||
* 4. 向用户发送会话加入成功响应
|
||||
* 5. 向会话中其他用户广播新用户加入通知
|
||||
*
|
||||
* @param client 已认证的WebSocket客户端
|
||||
* @param message 加入会话消息
|
||||
* 处理消息路由
|
||||
*/
|
||||
@SubscribeMessage('join_session')
|
||||
@UseGuards(WebSocketAuthGuard)
|
||||
@UsePipes(new ValidationPipe({ transform: true }))
|
||||
async handleJoinSession(
|
||||
@ConnectedSocket() client: AuthenticatedSocket,
|
||||
@MessageBody() message: JoinSessionMessage,
|
||||
) {
|
||||
private async handleMessage(client: ExtendedWebSocket, message: any) {
|
||||
const { event, data } = message;
|
||||
|
||||
switch (event) {
|
||||
case 'join_session':
|
||||
await this.handleJoinSession(client, data);
|
||||
break;
|
||||
case 'leave_session':
|
||||
await this.handleLeaveSession(client, data);
|
||||
break;
|
||||
case 'position_update':
|
||||
await this.handlePositionUpdate(client, data);
|
||||
break;
|
||||
case 'heartbeat':
|
||||
await this.handleHeartbeat(client, data);
|
||||
break;
|
||||
default:
|
||||
this.logger.warn('未知消息类型', {
|
||||
socketId: client.id,
|
||||
event,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理加入会话消息
|
||||
*/
|
||||
async handleJoinSession(client: ExtendedWebSocket, message: JoinSessionMessage) {
|
||||
const startTime = Date.now();
|
||||
|
||||
this.logger.log('处理加入会话请求', {
|
||||
@@ -288,6 +319,16 @@ export class LocationBroadcastGateway
|
||||
});
|
||||
|
||||
try {
|
||||
// 验证认证状态
|
||||
if (!client.userId) {
|
||||
throw new WsException({
|
||||
type: 'error',
|
||||
code: 'UNAUTHORIZED',
|
||||
message: '用户未认证',
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
// 1. 将用户添加到会话
|
||||
await this.locationBroadcastCore.addUserToSession(
|
||||
message.sessionId,
|
||||
@@ -343,7 +384,7 @@ export class LocationBroadcastGateway
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
client.emit('session_joined', joinResponse);
|
||||
this.sendMessage(client, 'session_joined', joinResponse);
|
||||
|
||||
// 5. 向会话中其他用户广播新用户加入通知
|
||||
const userJoinedNotification: UserJoinedNotification = {
|
||||
@@ -365,10 +406,10 @@ export class LocationBroadcastGateway
|
||||
};
|
||||
|
||||
// 广播给会话中的其他用户(排除当前用户)
|
||||
client.to(message.sessionId).emit('user_joined', userJoinedNotification);
|
||||
this.broadcastToSession(message.sessionId, 'user_joined', userJoinedNotification, client.id);
|
||||
|
||||
// 将客户端加入Socket.IO房间(用于广播)
|
||||
client.join(message.sessionId);
|
||||
// 将客户端加入会话房间
|
||||
this.joinRoom(client, message.sessionId);
|
||||
|
||||
const duration = Date.now() - startTime;
|
||||
this.logger.log('用户成功加入会话', {
|
||||
@@ -393,7 +434,7 @@ export class LocationBroadcastGateway
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
throw new WsException({
|
||||
const errorResponse: ErrorResponse = {
|
||||
type: 'error',
|
||||
code: 'JOIN_SESSION_FAILED',
|
||||
message: '加入会话失败',
|
||||
@@ -403,30 +444,16 @@ export class LocationBroadcastGateway
|
||||
},
|
||||
originalMessage: message,
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
};
|
||||
|
||||
this.sendMessage(client, 'error', errorResponse);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理离开会话消息
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 验证用户身份和会话权限
|
||||
* 2. 从会话中移除用户
|
||||
* 3. 清理用户相关数据
|
||||
* 4. 向会话中其他用户广播用户离开通知
|
||||
* 5. 发送离开成功确认
|
||||
*
|
||||
* @param client 已认证的WebSocket客户端
|
||||
* @param message 离开会话消息
|
||||
*/
|
||||
@SubscribeMessage('leave_session')
|
||||
@UseGuards(WebSocketAuthGuard)
|
||||
@UsePipes(new ValidationPipe({ transform: true }))
|
||||
async handleLeaveSession(
|
||||
@ConnectedSocket() client: AuthenticatedSocket,
|
||||
@MessageBody() message: LeaveSessionMessage,
|
||||
) {
|
||||
async handleLeaveSession(client: ExtendedWebSocket, message: LeaveSessionMessage) {
|
||||
const startTime = Date.now();
|
||||
|
||||
this.logger.log('处理离开会话请求', {
|
||||
@@ -439,6 +466,16 @@ export class LocationBroadcastGateway
|
||||
});
|
||||
|
||||
try {
|
||||
// 验证认证状态
|
||||
if (!client.userId) {
|
||||
throw new WsException({
|
||||
type: 'error',
|
||||
code: 'UNAUTHORIZED',
|
||||
message: '用户未认证',
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
// 1. 从会话中移除用户
|
||||
await this.locationBroadcastCore.removeUserFromSession(
|
||||
message.sessionId,
|
||||
@@ -454,10 +491,10 @@ export class LocationBroadcastGateway
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
client.to(message.sessionId).emit('user_left', userLeftNotification);
|
||||
this.broadcastToSession(message.sessionId, 'user_left', userLeftNotification, client.id);
|
||||
|
||||
// 3. 从Socket.IO房间中移除客户端
|
||||
client.leave(message.sessionId);
|
||||
// 3. 从会话房间中移除客户端
|
||||
this.leaveRoom(client, message.sessionId);
|
||||
|
||||
// 4. 发送离开成功确认
|
||||
const successResponse: SuccessResponse = {
|
||||
@@ -471,7 +508,7 @@ export class LocationBroadcastGateway
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
client.emit('leave_session_success', successResponse);
|
||||
this.sendMessage(client, 'leave_session_success', successResponse);
|
||||
|
||||
const duration = Date.now() - startTime;
|
||||
this.logger.log('用户成功离开会话', {
|
||||
@@ -496,7 +533,7 @@ export class LocationBroadcastGateway
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
throw new WsException({
|
||||
const errorResponse: ErrorResponse = {
|
||||
type: 'error',
|
||||
code: 'LEAVE_SESSION_FAILED',
|
||||
message: '离开会话失败',
|
||||
@@ -506,37 +543,23 @@ export class LocationBroadcastGateway
|
||||
},
|
||||
originalMessage: message,
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
};
|
||||
|
||||
this.sendMessage(client, 'error', errorResponse);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理位置更新消息
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 验证位置数据的有效性
|
||||
* 2. 更新用户在Redis中的位置缓存
|
||||
* 3. 获取用户当前所在的会话
|
||||
* 4. 向会话中其他用户广播位置更新
|
||||
* 5. 可选:触发位置数据持久化
|
||||
*
|
||||
* @param client 已认证的WebSocket客户端
|
||||
* @param message 位置更新消息
|
||||
*/
|
||||
@SubscribeMessage('position_update')
|
||||
@UseGuards(WebSocketAuthGuard)
|
||||
@UsePipes(new ValidationPipe({ transform: true }))
|
||||
async handlePositionUpdate(
|
||||
@ConnectedSocket() client: AuthenticatedSocket,
|
||||
@MessageBody() message: PositionUpdateMessage,
|
||||
) {
|
||||
async handlePositionUpdate(client: ExtendedWebSocket, message: PositionUpdateMessage) {
|
||||
// 开始性能监控
|
||||
const perfContext = this.performanceMonitor.startMonitoring('position_update', client);
|
||||
const perfContext = this.performanceMonitor.startMonitoring('position_update', client as any);
|
||||
|
||||
// 检查频率限制
|
||||
const rateLimitAllowed = this.rateLimitMiddleware.checkRateLimit(client.userId, client.id);
|
||||
const rateLimitAllowed = this.rateLimitMiddleware.checkRateLimit(client.userId || '', client.id);
|
||||
if (!rateLimitAllowed) {
|
||||
this.rateLimitMiddleware.handleRateLimit(client, client.userId);
|
||||
this.rateLimitMiddleware.handleRateLimit(client as any, client.userId || '');
|
||||
this.performanceMonitor.endMonitoring(perfContext, false, 'Rate limit exceeded');
|
||||
return;
|
||||
}
|
||||
@@ -554,6 +577,16 @@ export class LocationBroadcastGateway
|
||||
});
|
||||
|
||||
try {
|
||||
// 验证认证状态
|
||||
if (!client.userId) {
|
||||
throw new WsException({
|
||||
type: 'error',
|
||||
code: 'UNAUTHORIZED',
|
||||
message: '用户未认证',
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
// 1. 构建位置对象
|
||||
const position: Position = {
|
||||
userId: client.userId,
|
||||
@@ -567,32 +600,28 @@ export class LocationBroadcastGateway
|
||||
// 2. 更新用户位置
|
||||
await this.locationBroadcastCore.setUserPosition(client.userId, position);
|
||||
|
||||
// 3. 获取用户当前会话(从Redis中获取)
|
||||
// 注意:这里需要从Redis获取用户的会话信息
|
||||
// 暂时使用客户端房间信息作为会话ID
|
||||
const rooms = Array.from(client.rooms);
|
||||
const sessionId = rooms.find(room => room !== client.id); // 排除socket自身的房间
|
||||
// 3. 向用户所在的所有会话广播位置更新
|
||||
if (client.sessionIds) {
|
||||
for (const sessionId of client.sessionIds) {
|
||||
const positionBroadcast: PositionBroadcast = {
|
||||
type: 'position_broadcast',
|
||||
userId: client.userId,
|
||||
position: {
|
||||
x: position.x,
|
||||
y: position.y,
|
||||
mapId: position.mapId,
|
||||
timestamp: position.timestamp,
|
||||
metadata: position.metadata,
|
||||
},
|
||||
sessionId,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
if (sessionId) {
|
||||
// 4. 向会话中其他用户广播位置更新
|
||||
const positionBroadcast: PositionBroadcast = {
|
||||
type: 'position_broadcast',
|
||||
userId: client.userId,
|
||||
position: {
|
||||
x: position.x,
|
||||
y: position.y,
|
||||
mapId: position.mapId,
|
||||
timestamp: position.timestamp,
|
||||
metadata: position.metadata,
|
||||
},
|
||||
sessionId,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
client.to(sessionId).emit('position_update', positionBroadcast);
|
||||
this.broadcastToSession(sessionId, 'position_update', positionBroadcast, client.id);
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 发送位置更新成功确认(可选)
|
||||
// 4. 发送位置更新成功确认
|
||||
const successResponse: SuccessResponse = {
|
||||
type: 'success',
|
||||
message: '位置更新成功',
|
||||
@@ -606,7 +635,7 @@ export class LocationBroadcastGateway
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
client.emit('position_update_success', successResponse);
|
||||
this.sendMessage(client, 'position_update_success', successResponse);
|
||||
|
||||
const duration = Date.now() - startTime;
|
||||
this.logger.debug('位置更新处理完成', {
|
||||
@@ -614,7 +643,6 @@ export class LocationBroadcastGateway
|
||||
socketId: client.id,
|
||||
userId: client.userId,
|
||||
mapId: message.mapId,
|
||||
sessionId,
|
||||
duration,
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
@@ -637,7 +665,7 @@ export class LocationBroadcastGateway
|
||||
// 结束性能监控(失败)
|
||||
this.performanceMonitor.endMonitoring(perfContext, false, error instanceof Error ? error.message : String(error));
|
||||
|
||||
throw new WsException({
|
||||
const errorResponse: ErrorResponse = {
|
||||
type: 'error',
|
||||
code: 'POSITION_UPDATE_FAILED',
|
||||
message: '位置更新失败',
|
||||
@@ -647,28 +675,16 @@ export class LocationBroadcastGateway
|
||||
},
|
||||
originalMessage: message,
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
};
|
||||
|
||||
this.sendMessage(client, 'error', errorResponse);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理心跳消息
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 接收客户端心跳请求
|
||||
* 2. 更新连接活跃时间
|
||||
* 3. 返回服务端时间戳
|
||||
* 4. 重置连接超时计时器
|
||||
*
|
||||
* @param client WebSocket客户端
|
||||
* @param message 心跳消息
|
||||
*/
|
||||
@SubscribeMessage('heartbeat')
|
||||
@UsePipes(new ValidationPipe({ transform: true }))
|
||||
async handleHeartbeat(
|
||||
@ConnectedSocket() client: Socket,
|
||||
@MessageBody() message: HeartbeatMessage,
|
||||
) {
|
||||
async handleHeartbeat(client: ExtendedWebSocket, message: HeartbeatMessage) {
|
||||
this.logger.debug('处理心跳请求', {
|
||||
operation: 'heartbeat',
|
||||
socketId: client.id,
|
||||
@@ -678,21 +694,7 @@ export class LocationBroadcastGateway
|
||||
|
||||
try {
|
||||
// 1. 重置连接超时
|
||||
const timeout = (client as any).connectionTimeout;
|
||||
if (timeout) {
|
||||
clearTimeout(timeout);
|
||||
|
||||
// 重新设置超时
|
||||
const newTimeout = setTimeout(() => {
|
||||
this.logger.warn('客户端连接超时,自动断开', {
|
||||
socketId: client.id,
|
||||
timeout: `${LocationBroadcastGateway.CONNECTION_TIMEOUT_MINUTES}分钟`,
|
||||
});
|
||||
client.disconnect(true);
|
||||
}, LocationBroadcastGateway.CONNECTION_TIMEOUT_MINUTES * LocationBroadcastGateway.MILLISECONDS_PER_MINUTE);
|
||||
|
||||
(client as any).connectionTimeout = newTimeout;
|
||||
}
|
||||
this.setConnectionTimeout(client);
|
||||
|
||||
// 2. 构建心跳响应
|
||||
const heartbeatResponse: HeartbeatResponse = {
|
||||
@@ -703,7 +705,7 @@ export class LocationBroadcastGateway
|
||||
};
|
||||
|
||||
// 3. 发送心跳响应
|
||||
client.emit('heartbeat_response', heartbeatResponse);
|
||||
this.sendMessage(client, 'heartbeat_response', heartbeatResponse);
|
||||
|
||||
} catch (error) {
|
||||
this.logger.error('心跳处理失败', {
|
||||
@@ -711,31 +713,16 @@ export class LocationBroadcastGateway
|
||||
socketId: client.id,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
|
||||
// 心跳失败不抛出异常,避免断开连接
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理用户断开连接的清理工作
|
||||
*
|
||||
* 技术实现:
|
||||
* 1. 清理用户在所有会话中的数据
|
||||
* 2. 通知相关会话中的其他用户
|
||||
* 3. 清理Redis中的用户数据
|
||||
* 4. 记录断开连接的统计信息
|
||||
*
|
||||
* @param client 已认证的WebSocket客户端
|
||||
* @param reason 断开原因
|
||||
*/
|
||||
private async handleUserDisconnection(
|
||||
client: AuthenticatedSocket,
|
||||
reason: string,
|
||||
): Promise<void> {
|
||||
private async handleUserDisconnection(client: ExtendedWebSocket, reason: string): Promise<void> {
|
||||
try {
|
||||
// 1. 获取用户所在的所有房间(会话)
|
||||
const rooms = Array.from(client.rooms);
|
||||
const sessionIds = rooms.filter(room => room !== client.id);
|
||||
// 1. 获取用户所在的所有会话
|
||||
const sessionIds = Array.from(client.sessionIds || []);
|
||||
|
||||
// 2. 从所有会话中移除用户并通知其他用户
|
||||
for (const sessionId of sessionIds) {
|
||||
@@ -743,19 +730,19 @@ export class LocationBroadcastGateway
|
||||
// 从会话中移除用户
|
||||
await this.locationBroadcastCore.removeUserFromSession(
|
||||
sessionId,
|
||||
client.userId,
|
||||
client.userId!,
|
||||
);
|
||||
|
||||
// 通知会话中的其他用户
|
||||
const userLeftNotification: UserLeftNotification = {
|
||||
type: 'user_left',
|
||||
userId: client.userId,
|
||||
userId: client.userId!,
|
||||
reason,
|
||||
sessionId,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
client.to(sessionId).emit('user_left', userLeftNotification);
|
||||
this.broadcastToSession(sessionId, 'user_left', userLeftNotification, client.id);
|
||||
|
||||
} catch (error) {
|
||||
this.logger.error('从会话中移除用户失败', {
|
||||
@@ -768,7 +755,7 @@ export class LocationBroadcastGateway
|
||||
}
|
||||
|
||||
// 3. 清理用户的所有数据
|
||||
await this.locationBroadcastCore.cleanupUserData(client.userId);
|
||||
await this.locationBroadcastCore.cleanupUserData(client.userId!);
|
||||
|
||||
this.logger.log('用户断开连接清理完成', {
|
||||
socketId: client.id,
|
||||
@@ -787,4 +774,103 @@ export class LocationBroadcastGateway
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息给客户端
|
||||
*/
|
||||
private sendMessage(client: ExtendedWebSocket, event: string, data: any) {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
client.send(JSON.stringify({ event, data }));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 向会话房间广播消息
|
||||
*/
|
||||
private broadcastToSession(sessionId: string, event: string, data: any, excludeClientId?: string) {
|
||||
const room = this.sessionRooms.get(sessionId);
|
||||
if (!room) return;
|
||||
|
||||
for (const clientId of room) {
|
||||
if (excludeClientId && clientId === excludeClientId) continue;
|
||||
|
||||
const client = this.clients.get(clientId);
|
||||
if (client) {
|
||||
this.sendMessage(client, event, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将客户端加入会话房间
|
||||
*/
|
||||
private joinRoom(client: ExtendedWebSocket, sessionId: string) {
|
||||
if (!this.sessionRooms.has(sessionId)) {
|
||||
this.sessionRooms.set(sessionId, new Set());
|
||||
}
|
||||
|
||||
this.sessionRooms.get(sessionId)!.add(client.id);
|
||||
client.sessionIds!.add(sessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将客户端从会话房间移除
|
||||
*/
|
||||
private leaveRoom(client: ExtendedWebSocket, sessionId: string) {
|
||||
const room = this.sessionRooms.get(sessionId);
|
||||
if (room) {
|
||||
room.delete(client.id);
|
||||
if (room.size === 0) {
|
||||
this.sessionRooms.delete(sessionId);
|
||||
}
|
||||
}
|
||||
|
||||
client.sessionIds!.delete(sessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成客户端ID
|
||||
*/
|
||||
private generateClientId(): string {
|
||||
return `ws_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置连接超时
|
||||
*/
|
||||
private setConnectionTimeout(client: ExtendedWebSocket) {
|
||||
if (client.connectionTimeout) {
|
||||
clearTimeout(client.connectionTimeout);
|
||||
}
|
||||
|
||||
client.connectionTimeout = setTimeout(() => {
|
||||
this.logger.warn('客户端连接超时,自动断开', {
|
||||
socketId: client.id,
|
||||
timeout: `${LocationBroadcastGateway.CONNECTION_TIMEOUT_MINUTES}分钟`,
|
||||
});
|
||||
client.close();
|
||||
}, LocationBroadcastGateway.CONNECTION_TIMEOUT_MINUTES * LocationBroadcastGateway.MILLISECONDS_PER_MINUTE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置心跳检测
|
||||
*/
|
||||
private setupHeartbeat() {
|
||||
setInterval(() => {
|
||||
this.clients.forEach((client) => {
|
||||
if (!client.isAlive) {
|
||||
this.logger.warn('客户端心跳超时,断开连接', {
|
||||
socketId: client.id,
|
||||
});
|
||||
client.close();
|
||||
return;
|
||||
}
|
||||
|
||||
client.isAlive = false;
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
client.ping();
|
||||
}
|
||||
});
|
||||
}, LocationBroadcastGateway.HEARTBEAT_INTERVAL);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user