broke down ws

This commit is contained in:
2026-02-11 17:57:57 +08:00
parent 641f82ec49
commit c252e184a7
9 changed files with 529 additions and 339 deletions

View File

@@ -0,0 +1,55 @@
import { Logger } from '@nestjs/common';
import type { AuthenticatedSocket } from '../../../types/socket';
import { CursorPositionDto } from '../../dto/cursor-position.dto';
import { WS_EVENT } from '../ws-events';
import { Validator } from '../utils/validation';
import { Throttler } from '../utils/throttling';
import { Broadcaster } from '../utils/broadcasting';
const CURSOR_THROTTLE_MS = 100;
export class CursorHandler {
private readonly logger = new Logger(CursorHandler.name);
constructor(
private readonly broadcaster: Broadcaster,
private readonly throttler: Throttler,
) {}
async handleCursorReportPosition(
client: AuthenticatedSocket,
data: CursorPositionDto,
) {
Validator.validateUser(client);
const currentUserId = client.data.userId;
if (!currentUserId) {
// User has not initialized yet
return;
}
// Do not broadcast cursor position if user has no active doll
if (!client.data.activeDollId) {
return;
}
if (this.throttler.isThrottled(currentUserId, CURSOR_THROTTLE_MS)) {
return;
}
// Broadcast to online friends
const friends = client.data.friends;
if (friends) {
const payload = {
userId: currentUserId,
position: data,
};
await this.broadcaster.broadcastToFriends(
friends,
WS_EVENT.FRIEND_CURSOR_POSITION,
payload,
);
}
}
}