예제 #1
0
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
예제 #2
0
파일: verifier.py 프로젝트: uditsharma/hub
 def callback(channel):
     logger.debug(
         utils.get_client_address(request) + ' | ' + request.method +
         ' | /callback/' + channel + ' | ' + request.get_data().strip())
     return HubTasks.callback(channel)
예제 #3
0
파일: verifier.py 프로젝트: uditsharma/hub
 def get_channels():
     logger.debug(
         utils.get_client_address(request) + ' | ' + request.method +
         ' | /callback')
     return HubTasks.get_channels()
예제 #4
0
파일: batch.py 프로젝트: flightstats/hub
 def callback(channel):
     logger.debug(utils.get_client_address(request) + ' | ' + request.method + ' | /callback/' + channel + ' | ' + request.get_data().strip())
     return HubTasks.callback(channel)
예제 #5
0
파일: batch.py 프로젝트: flightstats/hub
 def get_channels():
     logger.debug(utils.get_client_address(request) + ' | ' + request.method + ' | /callback')
     return HubTasks.get_channels()