0
1
mirror of https://github.com/radio95-rnt/RadioPlayer.git synced 2026-02-26 21:53:54 +01:00

sync toplay better

This commit is contained in:
2025-12-13 23:03:59 +01:00
parent 9908fc818b
commit 0ca4ae509f
2 changed files with 21 additions and 16 deletions

View File

@@ -170,7 +170,7 @@
} else if(msg.event === "progress"){ } else if(msg.event === "progress"){
applyProgressState(msg.data); applyProgressState(msg.data);
} else if(msg.event === "toplay") { } else if(msg.event === "toplay") {
putQueue = msg.response.data || []; putQueue = msg.data.data || [];
renderPutQueue(); renderPutQueue();
} else if(msg.event === "request_dir") { } else if(msg.event === "request_dir") {
applySubdir(msg.data || {}) applySubdir(msg.data || {})
@@ -399,7 +399,6 @@
function sendAddToToplay(songs){ function sendAddToToplay(songs){
if(ws && ws.readyState === WebSocket.OPEN){ if(ws && ws.readyState === WebSocket.OPEN){
ws.send(JSON.stringify({action:"add_to_toplay", songs: songs})); ws.send(JSON.stringify({action:"add_to_toplay", songs: songs}));
setTimeout(() => ws.send(JSON.stringify({action:"get_toplay"})), 250);
} }
} }

View File

@@ -10,7 +10,7 @@ from . import Track, PlayerModule, Path
MAIN_PATH_DIR = Path("/home/user/mixes") MAIN_PATH_DIR = Path("/home/user/mixes")
async def ws_handler(websocket: ServerConnection, shared_data: dict, imc_q: multiprocessing.Queue): async def ws_handler(websocket: ServerConnection, shared_data: dict, imc_q: multiprocessing.Queue, ws_q: multiprocessing.Queue):
try: try:
initial = { initial = {
"playlist": json.loads(shared_data.get("playlist", "[]")), "playlist": json.loads(shared_data.get("playlist", "[]")),
@@ -27,6 +27,18 @@ async def ws_handler(websocket: ServerConnection, shared_data: dict, imc_q: mult
await websocket.send(json.dumps({"error": "invalid json"})) await websocket.send(json.dumps({"error": "invalid json"}))
continue continue
async def get_imc(name, data):
key = str(uuid.uuid4())
imc_q.put({"name": name, "data": data, "key": key})
start = time.monotonic()
result = None
while time.monotonic() - start < 2:
if key in shared_data:
result = shared_data.pop(key)
break
await asyncio.sleep(0.05)
return result
action = msg.get("action") action = msg.get("action")
if action == "skip": if action == "skip":
imc_q.put({"name": "procman", "data": {"op": 2}}) imc_q.put({"name": "procman", "data": {"op": 2}})
@@ -37,20 +49,14 @@ async def ws_handler(websocket: ServerConnection, shared_data: dict, imc_q: mult
else: else:
imc_q.put({"name": "activemod", "data": {"action": "add_to_toplay", "songs": songs}}) imc_q.put({"name": "activemod", "data": {"action": "add_to_toplay", "songs": songs}})
await websocket.send(json.dumps({"status": "ok", "message": f"{len(songs)} song(s) queued"})) await websocket.send(json.dumps({"status": "ok", "message": f"{len(songs)} song(s) queued"}))
result = await get_imc("activemod", {"action": "get_toplay"})
if result is not None:
await websocket.loop.run_in_executor(None, ws_q.put, ({"data": result, "event": "toplay"},))
elif action == "get_toplay": elif action == "get_toplay":
# replicate the previous behavior: send request to activemod and wait for keyed response result = await get_imc("activemod", {"action": "get_toplay"})
key = str(uuid.uuid4())
imc_q.put({"name": "activemod", "data": {"action": "get_toplay"}, "key": key})
# wait up to 2 seconds for shared_data[key] to appear
start = time.monotonic()
result = None
while time.monotonic() - start < 2:
if key in shared_data:
result = shared_data.pop(key)
break
await asyncio.sleep(0.05)
if result is None: await websocket.send(json.dumps({"error": "timeout", "code": 504})) if result is None: await websocket.send(json.dumps({"error": "timeout", "code": 504}))
else: await websocket.send(json.dumps({"status": "ok", "response": result, "event": "toplay"})) else: await websocket.send(json.dumps({"data": result, "event": "toplay"}))
elif action == "request_state": elif action == "request_state":
# supports requesting specific parts if provided # supports requesting specific parts if provided
what = msg.get("what", "") what = msg.get("what", "")
@@ -111,7 +117,7 @@ def websocket_server_process(shared_data: dict, imc_q: multiprocessing.Queue, ws
async def handler_wrapper(websocket: ServerConnection): async def handler_wrapper(websocket: ServerConnection):
# register client # register client
clients.add(websocket) clients.add(websocket)
try: await ws_handler(websocket, shared_data, imc_q) try: await ws_handler(websocket, shared_data, imc_q, ws_q)
finally: finally:
await websocket.close(1001, "") await websocket.close(1001, "")
clients.discard(websocket) clients.discard(websocket)