websockets: emit added, removed, and identified events

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2022-03-06 16:40:43 -05:00
parent 18169f279e
commit 7c1d6a8f24
No known key found for this signature in database
GPG Key ID: 7027245FBBDDF59A
1 changed files with 8 additions and 1 deletions

View File

@ -366,6 +366,7 @@ class WebsocketManager(APITransport):
f"Websocket {ws.uid} Client Identified - "
f"Name: {name}, Version: {version}, Type: {client_type}"
)
self.server.send_event("websockets:websocket_identified", ws)
return {'connection_id': ws.uid}
def has_websocket(self, ws_id: int) -> bool:
@ -401,12 +402,14 @@ class WebsocketManager(APITransport):
def add_websocket(self, ws: WebSocket) -> None:
self.websockets[ws.uid] = ws
self.server.send_event("websockets:websocked_added", ws)
logging.debug(f"New Websocket Added: {ws.uid}")
def remove_websocket(self, ws: WebSocket) -> None:
old_ws = self.websockets.pop(ws.uid, None)
if old_ws is not None:
self.klippy.remove_subscription(old_ws)
self.server.send_event("websockets:websocket_removed", ws)
logging.debug(f"Websocket Removed: {ws.uid}")
if self.closed_event is not None and not self.websockets:
self.closed_event.set()
@ -442,7 +445,7 @@ class WebSocket(WebSocketHandler, Subscribable):
self.event_loop = self.server.get_event_loop()
self.wsm: WebsocketManager = self.server.lookup_component("websockets")
self.rpc = self.wsm.rpc
self.uid = id(self)
self._uid = id(self)
self.is_closed: bool = False
self.ip_addr: str = self.request.remote_ip
self.queue_busy: bool = False
@ -451,6 +454,10 @@ class WebSocket(WebSocketHandler, Subscribable):
self._connected_time: float = 0.
self._client_data: Dict[str, str] = {}
@property
def uid(self) -> int:
return self._uid
@property
def hostname(self) -> str:
return self.request.host_name