From 7abc86847edbc6fef5589ae9e0738b489f0081a9 Mon Sep 17 00:00:00 2001 From: Arksine Date: Tue, 10 Nov 2020 07:13:10 -0500 Subject: [PATCH] websockets: register `server.websocket.id` remote method This allows clients to request the unique ID associated with each connected websocket. Signed-off-by: Eric Callahan --- moonraker/websockets.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/moonraker/websockets.py b/moonraker/websockets.py index c09ddc2..21bd74c 100644 --- a/moonraker/websockets.py +++ b/moonraker/websockets.py @@ -167,6 +167,8 @@ class WebsocketManager: self.ws_lock = tornado.locks.Lock() self.rpc = JsonRPC() + self.rpc.register_method("server.websocket.id", self._handle_id_request) + # Register events self.server.register_event_handler( "server:klippy_disconnect", self._handle_klippy_disconnect) @@ -223,9 +225,15 @@ class WebsocketManager: return result return func + async def _handle_id_request(self, ws, **kwargs): + return {'websocket_id': ws.uid} + def has_websocket(self, ws_id): return ws_id in self.websockets + def get_websocket(self, ws_id): + return self.websockets.get(ws_id, None) + async def add_websocket(self, ws): async with self.ws_lock: self.websockets[ws.uid] = ws