2021-02-26 15:39:35 +00:00
|
|
|
import json
|
2021-04-03 17:36:01 +00:00
|
|
|
import logging
|
2021-08-11 20:29:31 +00:00
|
|
|
import sys, threading, asyncio
|
2021-02-26 15:39:35 +00:00
|
|
|
|
|
|
|
from app.classes.shared.console import console
|
|
|
|
|
2021-04-03 17:36:01 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-08-11 20:29:31 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
import tornado.ioloop
|
|
|
|
|
|
|
|
except ModuleNotFoundError as e:
|
|
|
|
logger.critical("Import Error: Unable to load {} module".format(e, e.name))
|
|
|
|
console.critical("Import Error: Unable to load {} module".format(e, e.name))
|
|
|
|
sys.exit(1)
|
|
|
|
|
2021-02-26 15:39:35 +00:00
|
|
|
class WebSocketHelper:
|
2021-08-10 20:17:56 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.clients = set()
|
2021-02-26 15:39:35 +00:00
|
|
|
|
2021-08-10 20:17:56 +00:00
|
|
|
def add_client(self, client):
|
2021-02-26 15:39:35 +00:00
|
|
|
self.clients.add(client)
|
|
|
|
|
2021-08-10 20:17:56 +00:00
|
|
|
def remove_client(self, client):
|
|
|
|
self.clients.remove(client)
|
2021-08-11 20:29:31 +00:00
|
|
|
|
2021-08-10 20:17:56 +00:00
|
|
|
def send_message(self, client, event_type: str, data):
|
2021-03-01 17:33:15 +00:00
|
|
|
if client.check_auth():
|
|
|
|
message = str(json.dumps({'event': event_type, 'data': data}))
|
2021-08-11 20:29:31 +00:00
|
|
|
client.write_message_helper(message)
|
2021-02-26 15:39:35 +00:00
|
|
|
|
2021-08-10 20:17:56 +00:00
|
|
|
def broadcast(self, event_type: str, data):
|
|
|
|
logger.debug('Sending to {} clients: {}'.format(len(self.clients), json.dumps({'event': event_type, 'data': data})))
|
2021-02-26 15:39:35 +00:00
|
|
|
for client in self.clients:
|
|
|
|
try:
|
2021-03-01 00:54:20 +00:00
|
|
|
self.send_message(client, event_type, data)
|
2021-08-11 20:29:31 +00:00
|
|
|
except Exception as e:
|
|
|
|
logger.exception('Error catched while sending WebSocket message to {}'.format(client.get_remote_ip()))
|
2021-08-10 20:17:56 +00:00
|
|
|
|
|
|
|
def broadcast_page(self, page: str, event_type: str, data):
|
|
|
|
def filter_fn(client):
|
|
|
|
return client.page == page
|
|
|
|
|
|
|
|
clients = list(filter(filter_fn, self.clients))
|
|
|
|
|
|
|
|
logger.debug('Sending to {} out of {} clients: {}'.format(len(clients), len(self.clients), json.dumps({'event': event_type, 'data': data})))
|
|
|
|
|
|
|
|
for client in clients:
|
|
|
|
try:
|
|
|
|
self.send_message(client, event_type, data)
|
2021-08-11 20:29:31 +00:00
|
|
|
except Exception as e:
|
|
|
|
logger.exception('Error catched while sending WebSocket message to {}'.format(client.get_remote_ip()))
|
2021-08-10 20:17:56 +00:00
|
|
|
|
|
|
|
def broadcast_page_params(self, page: str, params: dict, event_type: str, data):
|
|
|
|
def filter_fn(client):
|
|
|
|
if client.page != page:
|
|
|
|
return False
|
|
|
|
for key, param in params.items():
|
|
|
|
if param != client.page_query_params.get(key, None):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
clients = list(filter(filter_fn, self.clients))
|
|
|
|
|
|
|
|
logger.debug('Sending to {} out of {} clients: {}'.format(len(clients), len(self.clients), json.dumps({'event': event_type, 'data': data})))
|
|
|
|
|
|
|
|
for client in clients:
|
|
|
|
try:
|
|
|
|
self.send_message(client, event_type, data)
|
2021-08-11 20:29:31 +00:00
|
|
|
except Exception as e:
|
|
|
|
logger.exception('Error catched while sending WebSocket message to {}'.format(client.get_remote_ip()))
|
2021-02-26 15:39:35 +00:00
|
|
|
|
|
|
|
def disconnect_all(self):
|
|
|
|
console.info('Disconnecting WebSocket clients')
|
|
|
|
for client in self.clients:
|
|
|
|
client.close()
|
|
|
|
console.info('Disconnected WebSocket clients')
|
|
|
|
|
|
|
|
websocket_helper = WebSocketHelper()
|