summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2019-12-10 05:53:29 +0300
committerDevaev Maxim <[email protected]>2019-12-10 05:53:29 +0300
commitf5a6fe35a99735c11eaad5d194061223bf9a0820 (patch)
tree5459afdd2856a4f9917d5f6c4aa95eb95300513c
parent3048fc79233f1ece909dee0f100e72a2f3f5a639 (diff)
ws api refactoring
-rw-r--r--kvmd/apps/kvmd/http.py4
-rw-r--r--kvmd/apps/kvmd/server.py21
-rw-r--r--web/share/js/kvm/keyboard.js5
-rw-r--r--web/share/js/kvm/mouse.js13
-rw-r--r--web/share/js/kvm/session.js28
5 files changed, 30 insertions, 41 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
diff --git a/web/share/js/kvm/keyboard.js b/web/share/js/kvm/keyboard.js
index d573d924..a443b2c9 100644
--- a/web/share/js/kvm/keyboard.js
+++ b/web/share/js/kvm/keyboard.js
@@ -126,9 +126,8 @@ export function Keyboard() {
tools.debug("Keyboard: key", (state ? "pressed:" : "released:"), code);
if (__ws) {
__ws.send(JSON.stringify({
- event_type: "key",
- key: code,
- state: state,
+ "event_type": "key",
+ "event": {"key": code, "state": state},
}));
}
};
diff --git a/web/share/js/kvm/mouse.js b/web/share/js/kvm/mouse.js
index 3b5da1de..1b8aa716 100644
--- a/web/share/js/kvm/mouse.js
+++ b/web/share/js/kvm/mouse.js
@@ -147,9 +147,8 @@ export function Mouse() {
__sendMove();
if (__ws) {
__ws.send(JSON.stringify({
- event_type: "mouse_button",
- button: button,
- state: state,
+ "event_type": "mouse_button",
+ "event": {"button": button, "state": state},
}));
}
};
@@ -166,8 +165,8 @@ export function Mouse() {
tools.debug("Mouse: moved:", to);
if (__ws) {
__ws.send(JSON.stringify({
- event_type: "mouse_move",
- to: to,
+ "event_type": "mouse_move",
+ "event": {"to": to},
}));
}
__sent_pos = pos;
@@ -202,8 +201,8 @@ export function Mouse() {
tools.debug("Mouse: scrolled:", delta);
if (__ws) {
__ws.send(JSON.stringify({
- event_type: "mouse_wheel",
- delta: delta,
+ "event_type": "mouse_wheel",
+ "event": {"delta": delta},
}));
}
}
diff --git a/web/share/js/kvm/session.js b/web/share/js/kvm/session.js
index cc9af52d..5df1481c 100644
--- a/web/share/js/kvm/session.js
+++ b/web/share/js/kvm/session.js
@@ -114,23 +114,15 @@ export function Session() {
var __wsMessageHandler = function(event) {
// tools.debug("Session: received socket data:", event.data);
- event = JSON.parse(event.data);
- if (event.msg_type === "pong") {
- __missed_heartbeats = 0;
- } else if (event.msg_type === "event") {
- if (event.msg.event === "info_state") {
- __setKvmdInfo(event.msg.event_attrs);
- } else if (event.msg.event === "hid_state") {
- __hid.setState(event.msg.event_attrs);
- } else if (event.msg.event === "atx_state") {
- __atx.setState(event.msg.event_attrs);
- } else if (event.msg.event === "msd_state") {
- __msd.setState(event.msg.event_attrs);
- } else if (event.msg.event === "streamer_state") {
- __streamer.setState(event.msg.event_attrs);
- } else if (event.msg.event === "wol_state") {
- __wol.setState(event.msg.event_attrs);
- }
+ let data = JSON.parse(event.data);
+ switch (data.event_type) {
+ case "pong": __missed_heartbeats = 0; break;
+ case "info_state": __setKvmdInfo(data.event); break;
+ case "wol_state": __wol.setState(data.event); break;
+ case "hid_state": __hid.setState(data.event); break;
+ case "atx_state": __atx.setState(data.event); break;
+ case "msd_state": __msd.setState(data.event); break;
+ case "streamer_state": __streamer.setState(data.event); break;
}
};
@@ -171,7 +163,7 @@ export function Session() {
if (__missed_heartbeats >= 5) {
throw new Error("Too many missed heartbeats");
}
- __ws.send(JSON.stringify({"event_type": "ping"}));
+ __ws.send(JSON.stringify({"event_type": "ping", "event": {}}));
} catch (err) {
tools.error("Session: ping error:", err.message);
if (__ws) {