채팅 연속 입력 전부 전송(짧은 연타 허용 + 보내기 대기열), 사람도 '입력 중' 표시, 오른쪽 위 소리 크기(0~100, 기본 50)
- 서버 방 채팅: 0.5초 고정 간격 대신 토큰 버킷(4줄 연속, 초당 2줄) — 도배는 여전히 막음 - 웹: useChatOutbox(방 채팅·마피아·라이어 게임)로 빠르게 친 줄도 순서대로 전송 - typing 메시지(방 채팅 / 게임 공개 채팅만, 마피아 밤 채널은 보내지 않음), BotActivity에 사람 표시 - VolumeControl: 사이트 머리글·방 막대 오른쪽 위, 설정 페이지에도 슬라이더 - e2e/chat.e2e.ts: 연타 7줄, 입력 중, 소리 크기 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
71
apps/web/src/lib/chat-input.ts
Normal file
71
apps/web/src/lib/chat-input.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* Chat input helpers shared by the room chat and the games with their own chat (마피아, 라이어 게임).
|
||||
*
|
||||
* - `useChatOutbox`: every line typed is sent, in order. The server allows a short burst and then about one line
|
||||
* per `gapMs`; lines typed faster than that wait in a small queue instead of being refused ("조금 천천히").
|
||||
* - `useTypingSignal`: tells the others "입력 중" while there is text in the box (renewed every 2 s, cleared when the
|
||||
* box is emptied or the line is sent, and when the box goes away).
|
||||
*/
|
||||
import { useCallback, useEffect, useRef } from 'react';
|
||||
import { socket } from '../net/socket';
|
||||
|
||||
const MAX_QUEUE = 10;
|
||||
|
||||
export function useChatOutbox<T = string>(send: (item: T) => void, gapMs: number) {
|
||||
const queue = useRef<T[]>([]);
|
||||
const last = useRef(-Infinity);
|
||||
const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const sendRef = useRef(send);
|
||||
sendRef.current = send;
|
||||
const pump = useCallback(() => {
|
||||
timer.current = null;
|
||||
const wait = last.current + gapMs - Date.now();
|
||||
if (wait > 0) {
|
||||
timer.current = setTimeout(pump, wait);
|
||||
return;
|
||||
}
|
||||
const next = queue.current.shift();
|
||||
if (next === undefined) return;
|
||||
last.current = Date.now();
|
||||
sendRef.current(next);
|
||||
if (queue.current.length) timer.current = setTimeout(pump, gapMs);
|
||||
}, [gapMs]);
|
||||
useEffect(() => () => void (timer.current && clearTimeout(timer.current)), []);
|
||||
return useCallback(
|
||||
(item: T) => {
|
||||
if (queue.current.length >= MAX_QUEUE) return;
|
||||
queue.current.push(item);
|
||||
if (!timer.current) pump();
|
||||
},
|
||||
[pump],
|
||||
);
|
||||
}
|
||||
|
||||
const RENEW_MS = 2000;
|
||||
|
||||
export function useTypingSignal(ch: 'room' | 'game', enabled = true) {
|
||||
const sentAt = useRef(0);
|
||||
const on = useRef(false);
|
||||
const stop = useCallback(() => {
|
||||
if (!on.current) return;
|
||||
on.current = false;
|
||||
sentAt.current = 0;
|
||||
socket.send({ t: 'typing', on: false, ch });
|
||||
}, [ch]);
|
||||
const onText = useCallback(
|
||||
(text: string) => {
|
||||
if (!enabled || !text.trim()) return stop();
|
||||
const now = Date.now();
|
||||
if (on.current && now - sentAt.current < RENEW_MS) return;
|
||||
on.current = true;
|
||||
sentAt.current = now;
|
||||
socket.send({ t: 'typing', on: true, ch });
|
||||
},
|
||||
[ch, enabled, stop],
|
||||
);
|
||||
useEffect(() => {
|
||||
if (!enabled) stop();
|
||||
}, [enabled, stop]);
|
||||
useEffect(() => stop, [stop]);
|
||||
return { onText, stop };
|
||||
}
|
||||
Reference in New Issue
Block a user