signaling-server.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python
  2. #
  3. # Python signaling server example for libdatachannel
  4. # Copyright (c) 2020 Paul-Louis Ageneau
  5. #
  6. # This Source Code Form is subject to the terms of the Mozilla Public
  7. # License, v. 2.0. If a copy of the MPL was not distributed with this
  8. # file, You can obtain one at https://mozilla.org/MPL/2.0/.
  9. import sys
  10. import ssl
  11. import json
  12. import asyncio
  13. import logging
  14. import websockets
  15. logger = logging.getLogger('websockets')
  16. logger.setLevel(logging.INFO)
  17. logger.addHandler(logging.StreamHandler(sys.stdout))
  18. clients = {}
  19. async def handle_websocket(websocket, path):
  20. client_id = None
  21. try:
  22. splitted = path.split('/')
  23. splitted.pop(0)
  24. client_id = splitted.pop(0)
  25. print('Client {} connected'.format(client_id))
  26. clients[client_id] = websocket
  27. while True:
  28. data = await websocket.recv()
  29. print('Client {} << {}'.format(client_id, data))
  30. message = json.loads(data)
  31. destination_id = message['id']
  32. destination_websocket = clients.get(destination_id)
  33. if destination_websocket:
  34. message['id'] = client_id
  35. data = json.dumps(message)
  36. print('Client {} >> {}'.format(destination_id, data))
  37. await destination_websocket.send(data)
  38. else:
  39. print('Client {} not found'.format(destination_id))
  40. except Exception as e:
  41. print(e)
  42. finally:
  43. if client_id:
  44. del clients[client_id]
  45. print('Client {} disconnected'.format(client_id))
  46. async def main():
  47. # Usage: ./server.py [[host:]port] [SSL certificate file]
  48. endpoint_or_port = sys.argv[1] if len(sys.argv) > 1 else "8000"
  49. ssl_cert = sys.argv[2] if len(sys.argv) > 2 else None
  50. endpoint = endpoint_or_port if ':' in endpoint_or_port else "127.0.0.1:" + endpoint_or_port
  51. if ssl_cert:
  52. ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
  53. ssl_context.load_cert_chain(ssl_cert)
  54. else:
  55. ssl_context = None
  56. print('Listening on {}'.format(endpoint))
  57. host, port = endpoint.rsplit(':', 1)
  58. server = await websockets.serve(handle_websocket, host, int(port), ssl=ssl_context)
  59. await server.wait_closed()
  60. if __name__ == '__main__':
  61. asyncio.run(main())