parley.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import asyncio
  2. import json
  3. import pysqlite3 as sql
  4. import sanic
  5. app = sanic.Sanic()
  6. @app.route("/static/<f>")
  7. async def static(request, f):
  8. return await sanic.response.file('static/{}'.format(f))
  9. @app.route("/")
  10. async def index(request):
  11. return await sanic.response.file('static/index.html')
  12. @app.websocket("/socket")
  13. async def socket(request, ws):
  14. initial = await ws.recv()
  15. config = json.loads(initial)
  16. sanic.log.logger.info('connected websocket for {} in game {}'.format(
  17. config['user'],
  18. config['game']))
  19. for x in range(50):
  20. await ws.send(json.dumps({"author": "server", "content": str(x)}))
  21. await asyncio.sleep(1)
  22. # await ws.send(json.dumps({"author": "server", "content": "two"}))
  23. # await asyncio.sleep(1)
  24. # await ws.send(json.dumps({"author": "server", "content": "three"}))
  25. # while True:
  26. # data = 'hello!'
  27. # print('Sending: ' + data)
  28. # await ws.send(data)
  29. # data = await ws.recv()
  30. # print('Received: ' + data)
  31. if __name__ == '__main__':
  32. app.run()