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 <arksine.code@gmail.com>
This commit is contained in:
Arksine 2020-11-10 07:13:10 -05:00
parent a6913a982a
commit 7abc86847e
1 changed files with 8 additions and 0 deletions

View File

@ -167,6 +167,8 @@ class WebsocketManager:
self.ws_lock = tornado.locks.Lock() self.ws_lock = tornado.locks.Lock()
self.rpc = JsonRPC() self.rpc = JsonRPC()
self.rpc.register_method("server.websocket.id", self._handle_id_request)
# Register events # Register events
self.server.register_event_handler( self.server.register_event_handler(
"server:klippy_disconnect", self._handle_klippy_disconnect) "server:klippy_disconnect", self._handle_klippy_disconnect)
@ -223,9 +225,15 @@ class WebsocketManager:
return result return result
return func return func
async def _handle_id_request(self, ws, **kwargs):
return {'websocket_id': ws.uid}
def has_websocket(self, ws_id): def has_websocket(self, ws_id):
return ws_id in self.websockets 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 def add_websocket(self, ws):
async with self.ws_lock: async with self.ws_lock:
self.websockets[ws.uid] = ws self.websockets[ws.uid] = ws