Anders and Briegel in Python
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
před 8 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/python
  2. """
  3. This is a simple websocket relay server that facilitates realtime visualization of GraphStates.
  4. It doesn't do much except echo websocket messages to all connected clients, and serve some static content over HTTP.
  5. """
  6. from websocket_server import WebsocketServer
  7. from SimpleHTTPServer import SimpleHTTPRequestHandler
  8. from BaseHTTPServer import HTTPServer
  9. from SocketServer import ThreadingMixIn
  10. import os, sys, threading
  11. import webbrowser
  12. import argparse
  13. import abp
  14. import json
  15. from pkg_resources import resource_filename
  16. clients = []
  17. def new_message(client, server, message):
  18. if message.startswith("edit:"):
  19. print message[5:]
  20. edit = json.loads(message[5:])
  21. print "Received update from javascript."
  22. print edit
  23. else:
  24. print "Received update from python {}.".format(client["id"])
  25. server.send_message_to_all(message)
  26. def new_client(client, server):
  27. print "Client {} connected.".format(client["id"])
  28. clients.append(client)
  29. def client_left(client, server):
  30. print "Client {} disconnected.".format(client["id"])
  31. clients.remove(client)
  32. class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
  33. """ Handle requests in a separate thread """
  34. if __name__ == '__main__':
  35. parser = argparse.ArgumentParser(description = "ABP websocket server")
  36. parser.add_argument("-v", action="store_true", help="Launch browser")
  37. args = parser.parse_args()
  38. # Change to the right working dir
  39. where = os.path.abspath(resource_filename("abp.static", ""))
  40. os.chdir(where)
  41. # Start the HTTP server
  42. httpserver = ThreadedHTTPServer(('', 5001), SimpleHTTPRequestHandler)
  43. thread = threading.Thread(target = httpserver.serve_forever)
  44. thread.daemon = True
  45. thread.start()
  46. if args.v:
  47. webbrowser.open("http://localhost:5001/")
  48. # Start the websocket server
  49. server = WebsocketServer(5000)
  50. server.set_fn_new_client(new_client)
  51. server.set_fn_message_received(new_message)
  52. server.set_fn_client_left(client_left)
  53. server.run_forever()
  54. httpserver.shutdown()