channel.py 442 B

123456789101112131415161718192021
  1. from typing import List
  2. class Channel:
  3. subscribers: List
  4. def __init__(self):
  5. self.subscribers = []
  6. def subscribe(self, ws):
  7. self.subscribers.append(ws)
  8. def unsubscribe(self, ws):
  9. self.subscribers.remove(ws)
  10. async def send(self, message):
  11. for ws in self.subscribers:
  12. if ws.open:
  13. await ws.send(message)
  14. else:
  15. self.unsubscribe(ws)