move connection tracking to all websockets

This commit is contained in:
Jakob Ketterl
2019-09-22 12:57:13 +02:00
parent b4ffc6e2f0
commit 57975b6f96
2 changed files with 19 additions and 3 deletions

View File

@ -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()