refactor: 更新WebSocket相关测试和location_broadcast模块
- 更新location_broadcast网关以支持原生WebSocket - 修改WebSocket认证守卫和中间件 - 更新相关的测试文件和规范 - 添加WebSocket测试工具 - 完善Zulip服务的测试覆盖 技术改进: - 统一WebSocket实现架构 - 优化性能监控和限流中间件 - 更新测试用例以适配新的WebSocket实现
This commit is contained in:
@@ -29,7 +29,14 @@
|
||||
*/
|
||||
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { Socket } from 'socket.io';
|
||||
|
||||
/**
|
||||
* 扩展的WebSocket接口
|
||||
*/
|
||||
interface ExtendedWebSocket extends WebSocket {
|
||||
id: string;
|
||||
userId?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 限流配置接口
|
||||
@@ -186,7 +193,7 @@ export class RateLimitMiddleware {
|
||||
* @param client WebSocket客户端
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
handleRateLimit(client: Socket, userId: string): void {
|
||||
handleRateLimit(client: ExtendedWebSocket, userId: string): void {
|
||||
const error = {
|
||||
type: 'error',
|
||||
code: 'RATE_LIMIT_EXCEEDED',
|
||||
@@ -199,7 +206,9 @@ export class RateLimitMiddleware {
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
client.emit('error', error);
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
client.send(JSON.stringify({ event: 'error', data: error }));
|
||||
}
|
||||
|
||||
this.logger.debug('发送限流错误响应', {
|
||||
userId,
|
||||
@@ -330,7 +339,7 @@ export function PositionUpdateRateLimit() {
|
||||
const method = descriptor.value;
|
||||
|
||||
descriptor.value = async function (...args: any[]) {
|
||||
const client = args[0] as Socket & { userId?: string };
|
||||
const client = args[0] as ExtendedWebSocket;
|
||||
const rateLimitMiddleware = new RateLimitMiddleware();
|
||||
|
||||
if (client.userId) {
|
||||
|
||||
Reference in New Issue
Block a user