4단계 방·대기실: 방 만들기(게스트 포함)·6자리 코드·열린 방 목록(🔒 비밀번호 방), 인원 무제한 대기실(방장·준비·넘기기·내보내기·60초 자리 유지), 방 설정 전체 즉시 반영, 조건에 맞는 곡 N곡 + 원곡 확인 중 곡 수(1분마다 다시 셈), 대기실 채팅, 서버 재시작 안내
- 서버 테스트 49개, 브라우저 e2e(e2e/rooms.e2e.ts, 3명) 11단계 - 사용자 결정(추천안)은 docs/12 "4단계 확인" Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -5,20 +5,28 @@
|
||||
import type { Server, ServerWebSocket } from 'bun';
|
||||
import { ClientMessage, MAX_CLIENT_MESSAGE_BYTES, PROTOCOL, type ServerMessage } from '@mq/shared';
|
||||
import type { LogFn } from '../server';
|
||||
import type { RoomManager } from '../rooms/manager';
|
||||
import { TokenBucket } from './rate-limit';
|
||||
|
||||
export interface WsData {
|
||||
userId: string;
|
||||
connId: number;
|
||||
/** The room this socket is in (docs/04). */
|
||||
roomCode?: string;
|
||||
}
|
||||
|
||||
export interface GatewayDeps {
|
||||
log: LogFn;
|
||||
onClose?: (userId: string) => void;
|
||||
rooms: RoomManager;
|
||||
/** Chat flood guard (docs/12 채팅): off by default, the admin turns it on in site settings. */
|
||||
chatGuard?: () => { on: boolean; ratePerSec: number; burst: number };
|
||||
}
|
||||
|
||||
export class Gateway {
|
||||
private conns = new Map<string, Set<ServerWebSocket<WsData>>>();
|
||||
private nextId = 1;
|
||||
private buckets = new Map<number, TokenBucket>();
|
||||
|
||||
constructor(private d: GatewayDeps) {}
|
||||
|
||||
@@ -57,7 +65,7 @@ export class Gateway {
|
||||
this.conns.set(ws.data.userId, set);
|
||||
this.send(ws, { t: 'welcome', protocol: PROTOCOL, userId: ws.data.userId, serverNow: Date.now() });
|
||||
},
|
||||
message: (ws: ServerWebSocket<WsData>, raw: string | Buffer) => {
|
||||
message: (ws: ServerWebSocket<WsData>, raw: string | Buffer): void => {
|
||||
const text = typeof raw === 'string' ? raw : raw.toString();
|
||||
if (text.length > MAX_CLIENT_MESSAGE_BYTES) return this.send(ws, { t: 'error', message: '메시지가 너무 길어요.' });
|
||||
let json: unknown;
|
||||
@@ -69,12 +77,41 @@ export class Gateway {
|
||||
const parsed = ClientMessage.safeParse(json);
|
||||
if (!parsed.success) return this.send(ws, { t: 'error', message: '잘못된 메시지예요.' });
|
||||
const m = parsed.data;
|
||||
const rooms = this.d.rooms;
|
||||
const err = (e: string | null): void => {
|
||||
if (e) this.send(ws, { t: 'error', message: e });
|
||||
};
|
||||
switch (m.t) {
|
||||
case 'ping':
|
||||
return this.send(ws, { t: 'pong', c: m.c, s: Date.now() });
|
||||
case 'join':
|
||||
return err(rooms.join(ws, m.code, m.password));
|
||||
case 'leave':
|
||||
return rooms.leave(ws);
|
||||
case 'config':
|
||||
return err(rooms.setConfig(ws, m.patch));
|
||||
case 'password':
|
||||
return err(rooms.setPassword(ws, m.value));
|
||||
case 'ready':
|
||||
return err(rooms.ready(ws, m.on));
|
||||
case 'kick':
|
||||
return err(rooms.kick(ws, m.userId));
|
||||
case 'host':
|
||||
return err(rooms.giveHost(ws, m.userId));
|
||||
case 'chat': {
|
||||
const g = this.d.chatGuard?.();
|
||||
if (g?.on) {
|
||||
let b = this.buckets.get(ws.data.connId);
|
||||
if (!b) this.buckets.set(ws.data.connId, (b = new TokenBucket(g.ratePerSec, g.burst)));
|
||||
if (!b.take()) return this.send(ws, { t: 'error', message: '채팅을 너무 빨리 보내고 있어요. 잠깐 쉬었다 보내 주세요.' });
|
||||
}
|
||||
return err(rooms.chat(ws, m.text, m.n));
|
||||
}
|
||||
}
|
||||
},
|
||||
close: (ws: ServerWebSocket<WsData>) => {
|
||||
this.buckets.delete(ws.data.connId);
|
||||
this.d.rooms.detach(ws);
|
||||
const set = this.conns.get(ws.data.userId);
|
||||
set?.delete(ws);
|
||||
if (set && set.size === 0) {
|
||||
|
||||
Reference in New Issue
Block a user