diff options
-rw-r--r-- | kvmd/kvmd/ps2.py | 8 | ||||
-rw-r--r-- | kvmd/kvmd/server.py | 14 | ||||
-rw-r--r-- | kvmd/web/index.html | 31 |
3 files changed, 35 insertions, 18 deletions
diff --git a/kvmd/kvmd/ps2.py b/kvmd/kvmd/ps2.py index 7f6bc85d..fe20e2c6 100644 --- a/kvmd/kvmd/ps2.py +++ b/kvmd/kvmd/ps2.py @@ -29,8 +29,12 @@ class Ps2Keyboard(multiprocessing.Process): self.__event.set() self.join() - def send_byte(self, code: int) -> None: - self.__queue.put(code) + def send_event(self, code: str, state: bool) -> None: + if state: + get_logger().info("Key pressed: %s", code) + else: + get_logger().info("Key released: %s", code) + # TODO: self.__queue.put(code) def cleanup(self) -> None: if self.is_alive(): diff --git a/kvmd/kvmd/server.py b/kvmd/kvmd/server.py index a49762d1..71a3a02c 100644 --- a/kvmd/kvmd/server.py +++ b/kvmd/kvmd/server.py @@ -124,11 +124,25 @@ class Server: # pylint: disable=too-many-instance-attributes aiohttp.web.run_app(app, host=host, port=port, print=self.__run_app_print) async def __ws_handler(self, request: aiohttp.web.Request) -> aiohttp.web.WebSocketResponse: + logger = get_logger(0) ws = aiohttp.web.WebSocketResponse(heartbeat=self.__heartbeat) await ws.prepare(request) await self.__register_socket(ws) async for msg in ws: if msg.type == aiohttp.web.WSMsgType.TEXT: + try: + event = json.loads(msg.data) + except Exception: + logger.exception("Can't parse JSON event from websocket") + else: + if event.get("event_type") == "key_event": + key_code = str(event.get("key_code", ""))[:64].strip() + key_state = event.get("key_state") + if key_code and key_state in [True, False]: + self.__keyboard.send_event(key_code, key_state) + continue + else: + logger.error("Invalid websocket event: %r", event) await ws.send_str(json.dumps({"msg_type": "echo", "msg": msg.data})) else: break diff --git a/kvmd/web/index.html b/kvmd/web/index.html index 87281e88..cb61185a 100644 --- a/kvmd/web/index.html +++ b/kvmd/web/index.html @@ -29,33 +29,32 @@ body { </head> <script> +ws = new WebSocket("ws://" + location.host + "/kvmd/ws"); + function onWsMessage(message) { console.log(message.data); - if (message.data == "EVENT mjpg_streamer started") { + /*if (message.data == "EVENT mjpg_streamer started") { document.getElementById("stream-image").src = "/streamer/?action=stream&time=" + new Date().getTime(); - } + }*/ } -function onKeyPress(event) { - console.log("key pressed: ", event); - let e = event || window.event || arguments.callee.caller.arguments[0]; -// ws.Send("EVENT press " + e.keyCode); +function onKeyEvent(event, state) { + // TODO: run this code under the lock + console.log("Key", (state ? "pressed:" : "released:"), event) + ws.send(JSON.stringify({ + event_type: "key_event", + key_code: event.code, + key_state: state, + })); } -function onKeyRelease(event) { - console.log("key released: ", event); - let e = event || window.event || arguments.callee.caller.arguments[0]; -// ws.Send("EVENT press " + e.keyCode); -} - -ws = new WebSocket("ws://" + location.host + "/kvmd/ws"); ws.onmessage = (message) => onWsMessage(message); ws.onerror = (error) => console.error(error); ws.onclose = () => console.log("closed"); -//https://www.codeday.top/2017/05/03/24906.html -document.onkeydown = onKeyPress; -document.onkeyup = onKeyRelease; +// https://www.codeday.top/2017/05/03/24906.html +document.onkeydown = (event) => onKeyEvent(event, true); +document.onkeyup = (event) => onKeyEvent(event, false); </script> <body> |