Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. import asyncio
  2. import websockets
  3. import logging
  4. import json
  5. import uuid
  6.  
  7. users = {}
  8.  
  9. logger = logging.getLogger('websockets.server')
  10. logger.setLevel(logging.DEBUG)
  11. logger.addHandler(logging.StreamHandler())
  12.  
  13. async def connect(websocket, path):
  14. logging.debug("connection at path: {}", path)
  15. if (path != "/connect"):
  16. return
  17.  
  18. id = uuid.uuid1()
  19. users[id] = websocket
  20.  
  21. try:
  22. async for message in websocket:
  23. logging.debug("message: {}", message)
  24. clients = [w for i, w in users.items() if i != id]
  25. await asyncio.wait([client.send(message) for client in clients])
  26. finally:
  27. del users[id]
  28.  
  29. server = websockets.serve(connect, "192.168.31.182", 8080)
  30. asyncio.get_event_loop().run_until_complete(server)
  31. asyncio.get_event_loop().run_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement