34 lines
850 B
Python
34 lines
850 B
Python
import asyncio
|
|
import json
|
|
import logging
|
|
from asyncio import Lock
|
|
from copy import deepcopy
|
|
from time import time
|
|
|
|
from geventwebsocket.websocket import WebSocket
|
|
|
|
|
|
class WebSocketClients:
|
|
def __init__(self):
|
|
self.clients = []
|
|
self.log = logging.getLogger(__name__)
|
|
|
|
def add(self, client):
|
|
self.clients.append(client)
|
|
|
|
def remove(self, client):
|
|
try:
|
|
client.close()
|
|
except Exception:
|
|
pass
|
|
if client in self.clients:
|
|
self.clients.remove(client)
|
|
|
|
def send(self, data):
|
|
for client in self.clients:
|
|
try:
|
|
client.send(json.dumps(data))
|
|
except Exception as e:
|
|
self.log.debug(f"Error sending data", exc_info=e)
|
|
if client in self.clients:
|
|
self.remove(client)
|