mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2024-08-30 18:23:09 +00:00
bd6eed118a
Also cleans up the panel and server code to handle other states better
36 lines
1.0 KiB
Python
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() |