crafty-4/app/classes/web/websocket_helper.py

36 lines
1.0 KiB
Python
Raw Normal View History

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