From 7e0f59233020aa55b65a8e427ddc8233cadeb947 Mon Sep 17 00:00:00 2001 From: LukasDoesDev Date: Mon, 18 Jan 2021 21:34:29 +0200 Subject: [PATCH] Delete code that will crash and change a bit of logging in websocket_handler.py --- app/classes/shared/tasks.py | 24 ------------------------ app/classes/web/websocket_handler.py | 9 +++++---- 2 files changed, 5 insertions(+), 28 deletions(-) diff --git a/app/classes/shared/tasks.py b/app/classes/shared/tasks.py index e24e08ce..8c5dc2cb 100644 --- a/app/classes/shared/tasks.py +++ b/app/classes/shared/tasks.py @@ -139,30 +139,6 @@ class TasksManager: @staticmethod def realtime_thread(): console.debug('realtime zero') - while True: - if len(WebSocketHandler.connections) > 0: - print(WebSocketHandler) - WebSocketHandler.broadcast(WebSocketHandler, 'sample_data', { - 'foo': 'bar', - 'baz': 'Hello, World!' - }) - - if WebSocketHandler.host_stats.get('cpu_usage') != \ - db_helper.get_latest_hosts_stats().get('cpu_usage') or \ - WebSocketHandler.host_stats.get('mem_percent') != \ - db_helper.get_latest_hosts_stats().get('mem_percent'): - - console.debug('realtime one') - WebSocketHandler.host_stats = db_helper.get_latest_hosts_stats() - if len(WebSocketHandler.connections) > 0: - WebSocketHandler.broadcast(WebSocketHandler, 'update_host_stats', { - 'cpu': WebSocketHandler.host_stats.get('cpu_usage'), - 'mem': WebSocketHandler.host_stats.get('mem_percent') - }) - time.sleep(4) - else: - console.debug('realtime two') - time.sleep(2) tasks_manager = TasksManager() diff --git a/app/classes/web/websocket_handler.py b/app/classes/web/websocket_handler.py index 1c3ff38c..3cb4192b 100644 --- a/app/classes/web/websocket_handler.py +++ b/app/classes/web/websocket_handler.py @@ -7,7 +7,6 @@ from app.classes.shared.models import db_helper class WebSocketHandler(tornado.websocket.WebSocketHandler): connections = set() - host_stats = db_helper.get_latest_hosts_stats() def open(self): self.connections.add(self) @@ -16,7 +15,7 @@ class WebSocketHandler(tornado.websocket.WebSocketHandler): 'foo': 'bar', }) - def on_message(self, message): + def on_message(self, rawMessage): # broadcast # for client in self.connections: # client.write_message(message) @@ -24,14 +23,16 @@ class WebSocketHandler(tornado.websocket.WebSocketHandler): # send message to client this message was sent by # self.write_message - console.debug('Got message from WebSocket connection {}'.format(message)) + console.debug('Got message from WebSocket connection {}'.format(rawMessage)) + message = json.loads(rawMessage) + console.debug('Type: {}, Data: {}'.format(message['type'], message['data'])) def on_close(self): self.connections.remove(self) console.debug('Closed WebSocket connection') def broadcast(self, message_type: str, data): - print(str(json.dumps({'type': message_type, 'data': data}))) + console.debug('Sending: ' + str(json.dumps({'type': message_type, 'data': data}))) message = str(json.dumps({'type': message_type, 'data': data})) for client in self.connections: client.write_message(message)