Delete code that will crash and change a bit of logging in websocket_handler.py

This commit is contained in:
LukasDoesDev 2021-01-18 21:34:29 +02:00
parent 6a253971e7
commit 7e0f592330
2 changed files with 5 additions and 28 deletions

View File

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

View File

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