diff options
| author | AAGaming <aa@mail.catvibers.me> | 2023-04-06 12:51:13 -0400 |
|---|---|---|
| committer | marios8543 <marios8543@gmail.com> | 2023-11-14 00:03:47 +0200 |
| commit | 18d89e76fd1f90066fbad08000c71ba149d2a4aa (patch) | |
| tree | 6251b486b4795fcd782513170d9aa7d7443aac05 /backend | |
| parent | 4a9b45b98e9540c7d83ba603e65197b79daa7c9b (diff) | |
| download | decky-loader-18d89e76fd1f90066fbad08000c71ba149d2a4aa.tar.gz decky-loader-18d89e76fd1f90066fbad08000c71ba149d2a4aa.zip | |
more work on websockets
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/decky_loader/loader.py | 2 | ||||
| -rw-r--r-- | backend/decky_loader/wsrouter.py | 47 |
2 files changed, 41 insertions, 8 deletions
diff --git a/backend/decky_loader/loader.py b/backend/decky_loader/loader.py index ea3a2e75..85b5de58 100644 --- a/backend/decky_loader/loader.py +++ b/backend/decky_loader/loader.py @@ -91,7 +91,7 @@ class Loader: self.ws = WSRouter() server_instance.web_app.add_routes([ - web.get("/socket", self.ws.handle), + web.get("/ws", self.ws.handle), web.get("/frontend/{path:.*}", self.handle_frontend_assets), web.get("/locales/{path:.*}", self.handle_frontend_locales), web.get("/plugins", self.get_plugins), diff --git a/backend/decky_loader/wsrouter.py b/backend/decky_loader/wsrouter.py index c501025e..84b83179 100644 --- a/backend/decky_loader/wsrouter.py +++ b/backend/decky_loader/wsrouter.py @@ -1,11 +1,32 @@ from logging import getLogger -from aiohttp import web +from asyncio import Future +from aiohttp import web, WSMsgType + +from enum import Enum + +from typing import Dict, Any, Callable + +from traceback import format_exc + +class MessageType(Enum): + # Call-reply + CALL = 0 + REPLY = 1 + ERROR = 2 + # Pub/sub + +running_calls: Dict[str, Future] = {} + +subscriptions: Dict[str, Callable[[Any]]] + +# {type: MessageType, data: dta, id: id} class WSRouter: def __init__(self) -> None: self.ws = None + self.req_id = 0 self.routes = {} self.logger = getLogger("WSRouter") @@ -23,16 +44,28 @@ class WSRouter: try: async for msg in ws: self.logger.debug(msg) - if msg.type == aiohttp.WSMsgType.TEXT: + if msg.type == WSMsgType.TEXT: self.logger.debug(msg.data) if msg.data == 'close': - # DO NOT RELY ON THIS! + # TODO DO NOT RELY ON THIS! break else: - # do stuff with the message - data = msg.json() - if self.routes[data.route]: - res = await self.routes[data.route](data.data) + match data.type: + case MessageType.CALL: + # do stuff with the message + data = msg.json() + if self.routes[data.route]: + try: + res = await self.routes[data.route](data.data) + await ws.send_json({type: MessageType.REPLY, id: data.id, data: res}) + except: + await ws.send_json({type: MessageType.ERROR, id: data.ud, data: format_exc()}) + case MessageType.REPLY: + if running_calls[data.id]: + running_calls[data.id].set_result(data.data) + case MessageType.ERROR: + if running_calls[data.id]: + running_calls[data.id].set_exception(data.data) finally: try: await ws.close() |
