diff --git a/src/components/syncPlay/controller.tsx b/src/components/syncPlay/controller.tsx index 815cff7..d874197 100644 --- a/src/components/syncPlay/controller.tsx +++ b/src/components/syncPlay/controller.tsx @@ -1,7 +1,10 @@ import { useNavigate } from "@tanstack/react-router"; import React, { useEffect } from "react"; import { getLocalPlayback } from "@/utils/methods/localPlayback"; -import { reportSyncPlayReady } from "@/utils/methods/syncPlay"; +import { + pingSyncPlayGroup, + reportSyncPlayReady, +} from "@/utils/methods/syncPlay"; import { useApiInContext } from "@/utils/store/api"; import { useCentralStore } from "@/utils/store/central"; import useSyncPlayStore from "@/utils/store/syncPlay"; @@ -28,6 +31,16 @@ const SyncPlayController = () => { }); }, [pendingNavigation, setPendingNavigation, navigate]); + // SyncPlay uses pings to estimate latency and schedule unpause commands. + useEffect(() => { + if (!api || !group?.GroupId) return; + void pingSyncPlayGroup(api); + const timer = window.setInterval(() => { + void pingSyncPlayGroup(api); + }, 3000); + return () => window.clearInterval(timer); + }, [api, group?.GroupId]); + // While a group is waiting, keep telling the server this player is ready. // Some clients miss the one-shot Ready after a queue update; retrying is // harmless and lets the server release the group as soon as it can. diff --git a/src/utils/methods/syncPlay.ts b/src/utils/methods/syncPlay.ts index e39c344..a1fce3d 100644 --- a/src/utils/methods/syncPlay.ts +++ b/src/utils/methods/syncPlay.ts @@ -219,6 +219,19 @@ export const pauseSyncPlayGroup = async (api: Api) => { } }; +export const pingSyncPlayGroup = async (api: Api) => { + try { + await syncApi(api).syncPlayPing({ + // This is the measured latency in milliseconds, not a timestamp. + // The server clamps it to 10s; a huge value would delay every + // scheduled pause/unpause, so report a conservative local value. + pingRequestDto: { Ping: 50 }, + }); + } catch (error) { + console.warn("Could not ping SyncPlay group", error); + } +}; + export const resumeSyncPlayGroup = async (api: Api) => { try { await syncApi(api).syncPlayUnpause();