def wshandler(request):
print("Connected")
app = request.app
game = app["game"]
ws = web.WebSocketResponse()
await ws.prepare(request)
player = None
while True:
msg = await ws.receive()
if msg.tp == web.MsgType.text:
print("Got message %s" % msg.data)
data = json.loads(msg.data)
if type(data) == int and player:
# Interpret as key code
player.keypress(data)
if type(data) != list:
continue
if not player:
if data[0] == "new_player":
player = game.new_player(data[1], ws)
elif data[0] == "join":
if not game.running:
game.reset_world()
print("Starting game loop")
asyncio.ensure_future(game_loop(game))
game.join(player)
elif msg.tp == web.MsgType.close:
break
if player:
game.player_disconnected(player)
print("Closed connection")
return ws
评论列表
文章目录