refactor: 更新WebSocket相关测试和location_broadcast模块

- 更新location_broadcast网关以支持原生WebSocket
- 修改WebSocket认证守卫和中间件
- 更新相关的测试文件和规范
- 添加WebSocket测试工具
- 完善Zulip服务的测试覆盖

技术改进:
- 统一WebSocket实现架构
- 优化性能监控和限流中间件
- 更新测试用例以适配新的WebSocket实现
This commit is contained in:
moyin
2026-01-09 17:02:43 +08:00
parent e9dc887c59
commit cbf4120ddd
13 changed files with 752 additions and 524 deletions

View File

@@ -29,7 +29,14 @@
*/
import { Injectable, Logger } from '@nestjs/common';
import { Socket } from 'socket.io';
/**
* 扩展的WebSocket接口
*/
interface ExtendedWebSocket extends WebSocket {
id: string;
userId?: string;
}
/**
* 性能指标接口
@@ -203,7 +210,7 @@ export class PerformanceMonitorMiddleware {
* @param client WebSocket客户端
* @returns 监控上下文
*/
startMonitoring(eventName: string, client: Socket): { startTime: [number, number]; eventName: string; client: Socket } {
startMonitoring(eventName: string, client: ExtendedWebSocket): { startTime: [number, number]; eventName: string; client: ExtendedWebSocket } {
const startTime = process.hrtime();
// 记录连接
@@ -220,7 +227,7 @@ export class PerformanceMonitorMiddleware {
* @param error 错误信息
*/
endMonitoring(
context: { startTime: [number, number]; eventName: string; client: Socket },
context: { startTime: [number, number]; eventName: string; client: ExtendedWebSocket },
success: boolean = true,
error?: string,
): void {
@@ -231,7 +238,7 @@ export class PerformanceMonitorMiddleware {
eventName: context.eventName,
duration,
timestamp: Date.now(),
userId: (context.client as any).userId,
userId: context.client.userId,
socketId: context.client.id,
success,
error,
@@ -246,7 +253,7 @@ export class PerformanceMonitorMiddleware {
* @param client WebSocket客户端
* @param connected 是否连接
*/
recordConnection(client: Socket, connected: boolean): void {
recordConnection(client: ExtendedWebSocket, connected: boolean): void {
if (connected) {
this.connectionCount++;
this.activeConnections.add(client.id);
@@ -640,7 +647,7 @@ export function PerformanceMonitor(eventName?: string) {
const finalEventName = eventName || propertyName;
descriptor.value = async function (...args: any[]) {
const client = args[0] as Socket;
const client = args[0] as ExtendedWebSocket;
const performanceMonitor = new PerformanceMonitorMiddleware();
const context = performanceMonitor.startMonitoring(finalEventName, client);