fix: sync auction timer with server time
This commit is contained in:
1 parent
d2fe4604dd
commit
98c2323ff2
2 files changed
+24
-1
No files matched your search
@@ -30,6 +30,11 @@ interface ResetPayload {
|
||||
matchId?: string;
|
||||
}
|
||||
|
||||
interface TickPayload {
|
||||
deadline?: string;
|
||||
serverTime?: string;
|
||||
}
|
||||
|
||||
const MAX_CHAT_MESSAGES = 200;
|
||||
|
||||
function mergeChatMessages(
|
||||
@@ -55,6 +60,7 @@ export function useAuctionSocket(matchId: string | undefined) {
|
||||
const [connectionStatus, setConnectionStatus] =
|
||||
useState<ConnectionStatus>('idle');
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [serverTimeOffsetMs, setServerTimeOffsetMs] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
if (!matchId) {
|
||||
@@ -100,6 +106,16 @@ export function useAuctionSocket(matchId: string | undefined) {
|
||||
setError(null);
|
||||
});
|
||||
|
||||
socket.on('tick', (payload: TickPayload) => {
|
||||
const serverTime =
|
||||
typeof payload?.serverTime === 'string'
|
||||
? Date.parse(payload.serverTime)
|
||||
: NaN;
|
||||
if (Number.isFinite(serverTime)) {
|
||||
setServerTimeOffsetMs(serverTime - Date.now());
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('chat-history', (payload: ChatHistoryPayload) => {
|
||||
setChatMessages(mergeChatMessages([], payload.messages ?? []));
|
||||
});
|
||||
@@ -129,6 +145,7 @@ export function useAuctionSocket(matchId: string | undefined) {
|
||||
socket?.disconnect();
|
||||
socketRef.current = null;
|
||||
setConnectionStatus('idle');
|
||||
setServerTimeOffsetMs(0);
|
||||
};
|
||||
}, [authUserId, matchId]);
|
||||
|
||||
@@ -160,6 +177,7 @@ export function useAuctionSocket(matchId: string | undefined) {
|
||||
resetAt,
|
||||
connectionStatus: visibleConnectionStatus,
|
||||
error,
|
||||
serverTimeOffsetMs: matchId ? serverTimeOffsetMs : 0,
|
||||
placeBid(payload: BidPayload) {
|
||||
emit('bid', payload);
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user