diff options
author | Devaev Maxim <[email protected]> | 2019-12-10 05:53:29 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2019-12-10 05:53:29 +0300 |
commit | f5a6fe35a99735c11eaad5d194061223bf9a0820 (patch) | |
tree | 5459afdd2856a4f9917d5f6c4aa95eb95300513c /kvmd/apps | |
parent | 3048fc79233f1ece909dee0f100e72a2f3f5a639 (diff) |
ws api refactoring
Diffstat (limited to 'kvmd/apps')
-rw-r--r-- | kvmd/apps/kvmd/http.py | 4 | ||||
-rw-r--r-- | kvmd/apps/kvmd/server.py | 21 |
2 files changed, 12 insertions, 13 deletions
diff --git a/kvmd/apps/kvmd/http.py b/kvmd/apps/kvmd/http.py index ce263063..a33eab75 100644 --- a/kvmd/apps/kvmd/http.py +++ b/kvmd/apps/kvmd/http.py @@ -64,7 +64,7 @@ def get_exposed_http(obj: object) -> List[HttpExposed]: handler=handler, ) for name in dir(obj) - if inspect.ismethod(handler := getattr(obj, name)) and getattr(handler, _HTTP_EXPOSED, False) + if inspect.ismethod(handler := getattr(obj, name)) and getattr(handler, _HTTP_EXPOSED, False) # noqa: E203 ] @@ -94,7 +94,7 @@ def get_exposed_ws(obj: object) -> List[WsExposed]: handler=handler, ) for name in dir(obj) - if inspect.ismethod(handler := getattr(obj, name)) and getattr(handler, _WS_EXPOSED, False) + if inspect.ismethod(handler := getattr(obj, name)) and getattr(handler, _WS_EXPOSED, False) # noqa: E203 ] diff --git a/kvmd/apps/kvmd/server.py b/kvmd/apps/kvmd/server.py index 70d02582..d4d35601 100644 --- a/kvmd/apps/kvmd/server.py +++ b/kvmd/apps/kvmd/server.py @@ -249,22 +249,24 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins async for msg in ws: if msg.type == aiohttp.web.WSMsgType.TEXT: try: - event = json.loads(msg.data) + data = json.loads(msg.data) + event_type = data.get("event_type") + event = data["event"] except Exception as err: - logger.error("Can't parse JSON event from websocket: %s", err) + logger.error("Can't parse JSON event from websocket: %r", err) else: - handler = self.__ws_handlers.get(event.get("event_type")) + handler = self.__ws_handlers.get(event_type) if handler: await handler(ws, event) else: - logger.error("Unknown websocket event: %r", event) + logger.error("Unknown websocket event: %r", data) else: break return ws @exposed_ws("ping") async def __ws_ping_handler(self, ws: aiohttp.web.WebSocketResponse, _: Dict) -> None: - await ws.send_str(json.dumps({"msg_type": "pong"})) + await ws.send_str(json.dumps({"event_type": "pong", "event": {}})) # ===== SYSTEM STUFF @@ -377,15 +379,12 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins except Exception: logger.exception("Cleanup error on %s", name) - async def __broadcast_event(self, event_type: _Events, event_attrs: Dict) -> None: + async def __broadcast_event(self, event_type: _Events, event: Dict) -> None: if self.__sockets: await asyncio.gather(*[ ws.send_str(json.dumps({ - "msg_type": "event", - "msg": { - "event": event_type.value, - "event_attrs": event_attrs, - }, + "event_type": event_type.value, + "event": event, })) for ws in list(self.__sockets) if not ws.closed and ws._req is not None and ws._req.transport is not None # pylint: disable=protected-access |