From 57975b6f96337786613079ab3385f97c54acd680 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Sun, 22 Sep 2019 12:57:13 +0200 Subject: [PATCH] move connection tracking to all websockets --- openwebrx.py | 6 +++--- owrx/websocket.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/openwebrx.py b/openwebrx.py index a013349..d4ebddd 100755 --- a/openwebrx.py +++ b/openwebrx.py @@ -4,10 +4,11 @@ from http.server import HTTPServer from owrx.http import RequestHandler from owrx.config import PropertyManager from owrx.feature import FeatureDetector -from owrx.source import SdrService, ClientRegistry +from owrx.source import SdrService from socketserver import ThreadingMixIn from owrx.sdrhu import SdrHuUpdater from owrx.service import Services +from owrx.websocket import WebSocketConnection import logging @@ -58,5 +59,4 @@ if __name__ == "__main__": try: main() except KeyboardInterrupt: - for c in ClientRegistry.getSharedInstance().clients: - c.close() + WebSocketConnection.closeAll() diff --git a/owrx/websocket.py b/owrx/websocket.py index dad40cd..d48784f 100644 --- a/owrx/websocket.py +++ b/owrx/websocket.py @@ -15,6 +15,16 @@ class IncompleteRead(Exception): class WebSocketConnection(object): + connections = [] + + @staticmethod + def closeAll(): + for c in WebSocketConnection.connections: + try: + c.close() + except: + logger.exception("exception while shutting down websocket connections") + def __init__(self, handler, messageHandler): self.handler = handler self.handler.connection.setblocking(0) @@ -121,6 +131,7 @@ class WebSocketConnection(object): self.interruptPipeSend.send(bytes(0x00)) def read_loop(self): + WebSocketConnection.connections.append(self) self.open = True while self.open: (read, _, _) = select.select([self.interruptPipeRecv, self.handler.rfile], [], []) @@ -168,6 +179,11 @@ class WebSocketConnection(object): except OSError: logger.exception("OSError while writing close frame:") + try: + WebSocketConnection.connections.remove(self) + except ValueError: + pass + def close(self): self.open = False self.interrupt()