채팅을 따로 분리해 속도 제한 없이: 방 채팅·마피아·라이어·그림 맞히기(그리기 전) 채팅의 줄 간격 제한(chat-pace)과 브라우저 대기열을 없애 치는 대로 바로 보냄. 서버 연결당 공용 한도(초당 20개)를 채팅·입력 중 신호가 같이 써서 빠르게 치면 15번째 줄부터 조용히 사라지던 문제도: 채팅은 별도 한도(초당 50, 최대 200, 매크로 도배 방지용)로 분리. 게임 채팅은 보내는 동안 게임 버튼을 잠그지 않음(pending 미설정)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,50 +1,13 @@
|
||||
/**
|
||||
* Chat input helpers shared by the room chat and the games with their own chat (마피아, 라이어 게임).
|
||||
* Chat input helper shared by the room chat and the games with their own chat (마피아, 라이어 게임).
|
||||
*
|
||||
* - `useChatOutbox`: every line typed is sent right away, in order. Only past the server's chat pace (`chat-pace.ts`,
|
||||
* a burst of lines and then several a second) do lines wait in a small queue instead of being refused ("조금 천천히").
|
||||
* Chat lines are sent the moment Enter is pressed — no queue and no pace limit.
|
||||
* - `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 { CHAT_BURST, CHAT_INTERVAL_MS, chatNext, chatWait } from '@bg/games/chat-pace';
|
||||
import { socket } from '../net/socket';
|
||||
|
||||
const MAX_QUEUE = 10;
|
||||
// A little under the server's pace, so lines bunched up on the way (network jitter) are not refused there.
|
||||
const BURST = CHAT_BURST - 1;
|
||||
const INTERVAL = CHAT_INTERVAL_MS + 40;
|
||||
|
||||
export function useChatOutbox<T = string>(send: (item: T) => void) {
|
||||
const queue = useRef<T[]>([]);
|
||||
const next = useRef<number | undefined>(undefined);
|
||||
const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const sendRef = useRef(send);
|
||||
sendRef.current = send;
|
||||
const pump = useCallback(() => {
|
||||
timer.current = null;
|
||||
while (queue.current.length) {
|
||||
const now = Date.now();
|
||||
const wait = chatWait(next.current, now, BURST, INTERVAL);
|
||||
if (wait > 0) {
|
||||
timer.current = setTimeout(pump, wait);
|
||||
return;
|
||||
}
|
||||
next.current = chatNext(next.current, now, INTERVAL);
|
||||
sendRef.current(queue.current.shift()!);
|
||||
}
|
||||
}, []);
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user