채팅 연타가 몇 초씩 밀리던 문제: 브라우저가 0.55초(마피아·라이어 0.75초)에 한 줄씩만 내보내던 대기열과 게임 채팅 0.7초 간격 제한을 없애고, 방 채팅·마피아·라이어가 같은 속도 규칙(한 번에 6줄, 그 뒤 0.3초에 한 줄)을 쓰게. 사람이 치는 속도로는 기다리지 않고 바로 올라감
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,35 +1,39 @@
|
||||
/**
|
||||
* 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 ("조금 천천히").
|
||||
* - `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 ("조금 천천히").
|
||||
* - `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, gapMs: number) {
|
||||
export function useChatOutbox<T = string>(send: (item: T) => void) {
|
||||
const queue = useRef<T[]>([]);
|
||||
const last = useRef(-Infinity);
|
||||
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;
|
||||
const wait = last.current + gapMs - Date.now();
|
||||
if (wait > 0) {
|
||||
timer.current = setTimeout(pump, wait);
|
||||
return;
|
||||
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()!);
|
||||
}
|
||||
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) => {
|
||||
|
||||
Reference in New Issue
Block a user