async def ws_handler(request): client_address = get_client_address(request) game = request.app['game'] player = None ws = web.WebSocketResponse() await ws.prepare(request) async for msg in ws: if msg.type == WSMsgType.TEXT: try: data = json.loads(msg.data) except ValueError: continue if isinstance(data, int) and player: player.keypress(data) elif not isinstance(data, list) or not data: continue elif data[0] == Messaging.MSG_PING: await ws.send_json([Messaging.MSG_PONG] + data[1:], dumps=json.dumps) elif data[0] == Messaging.MSG_NEW_PLAYER: if not player: try: player_name, player_id = _get_new_player_info(data) except ValidationError as exc: await ws.send_json([Messaging.MSG_ERROR, str(exc)]) break else: player = await game.new_player(player_name, ws, player_id=player_id) elif data[0] == Messaging.MSG_JOIN: if not game.running: await game.reset_world() try: asyncio.ensure_future(game_loop(game)) except asyncio.CancelledError: pass await game.join(player) elif msg.type == WSMsgType.CLOSE: break if player: await game.player_disconnected(player) return ws
def callback(channel): logger.debug( utils.get_client_address(request) + ' | ' + request.method + ' | /callback/' + channel + ' | ' + request.get_data().strip()) return HubTasks.callback(channel)
def get_channels(): logger.debug( utils.get_client_address(request) + ' | ' + request.method + ' | /callback') return HubTasks.get_channels()
def callback(channel): logger.debug(utils.get_client_address(request) + ' | ' + request.method + ' | /callback/' + channel + ' | ' + request.get_data().strip()) return HubTasks.callback(channel)
def get_channels(): logger.debug(utils.get_client_address(request) + ' | ' + request.method + ' | /callback') return HubTasks.get_channels()