crafty-4/app/classes/web/websocket_helper.py
computergeek125 bd6eed118a Resolves CRAFTY-CONTROLLER-T-81 - error when creating new user
Also cleans up the panel and server code to handle other states better
2021-04-03 12:36:01 -05:00

36 lines
1.0 KiB
Python

import json
import logging
from app.classes.shared.console import console
logger = logging.getLogger(__name__)
class WebSocketHelper:
clients = set()
def addClient(self, client):
self.clients.add(client)
def removeClient(self, client):
self.clients.add(client)
def send_message(self, client, event_type, data):
if client.check_auth():
message = str(json.dumps({'event': event_type, 'data': data}))
client.write_message(message)
def broadcast(self, event_type, data):
logger.debug('Sending: ' + str(json.dumps({'event': event_type, 'data': data})))
for client in self.clients:
try:
self.send_message(client, event_type, data)
except:
pass
def disconnect_all(self):
console.info('Disconnecting WebSocket clients')
for client in self.clients:
client.close()
console.info('Disconnected WebSocket clients')
websocket_helper = WebSocketHelper()