示例#1
0
文件: routes.py 项目: camuschat/camus
async def room_ws(room_id):
    # Verify that the room exists
    Room.query.filter_by(slug=room_id).first_or_404()

    # Verify the client using a secure cookie
    client = Client.query.filter_by(uuid=session.get('id', None)).first()
    if client:
        logging.info(f'Accepted websocket connection for client {client.uuid}')
        await websocket.accept()
    else:
        return 'Forbidden', 403

    inbox, outbox = message_handler.inbox, message_handler.outbox

    send_task = asyncio.create_task(
        copy_current_websocket_context(ws_send)(outbox[client.uuid]), )
    receive_task = asyncio.create_task(
        copy_current_websocket_context(ws_receive)(client.uuid, inbox), )
    try:
        await asyncio.gather(send_task, receive_task)
    finally:
        logging.info(
            f'Terminating websocket connection for client {client.uuid}')
        send_task.cancel()
        receive_task.cancel()
示例#2
0
async def ws():
    if args.verbosity > 1: print(f'{OV}entered ws(){OM}')
    await broadcast('{"Type":"System","Data":"Someone connected"}')
    # await broadcast(b'{"Type":"System","Data":"This is a byte string yo!"}')
    consumer_task = asyncio.ensure_future( # copy websocket to consumer()
        copy_current_websocket_context(consumer)(),
    )
    producer_task = asyncio.ensure_future( # copy websocket to producer()
        copy_current_websocket_context(producer)(),
    )
    try:
        result = await asyncio.gather(consumer_task, producer_task)
        if args.verbosity > 1: print(f'{OV}result is {OR}{result}{OM}')
    finally:
        consumer_task.cancel()
        producer_task.cancel()
示例#3
0
async def chat_room_ws(room_id):
    manager = chat.get_chat_manager()
    room = manager.get_room(room_id)

    if room is None:
        return  # close the websocket

    client = manager.create_client()
    room.add_client(client)

    send_task = asyncio.create_task(
        copy_current_websocket_context(ws_send)(client.outbox), )
    receive_task = asyncio.create_task(
        copy_current_websocket_context(ws_receive)(client.inbox), )
    try:
        await asyncio.gather(send_task, receive_task)
    finally:
        send_task.cancel()
        receive_task.cancel()
示例#4
0
async def channel_feed_websocket():
    obj = websocket._get_current_object()

    feeds.add(obj)

    consumer = asyncio.ensure_future(
        copy_current_websocket_context(receiving)(), )
    try:
        await asyncio.gather(consumer)
    finally:
        consumer.cancel()
        feeds.remove(obj)
示例#5
0
async def ws(room_id):
    obj = websocket._get_current_object()

    # Put connection in a dict
    connections[obj] = room_id

    room = create_room(room_id)

    await room.new_member(obj)

    # Tie sending loop to the connection
    consumer = asyncio.ensure_future(
        copy_current_websocket_context(receiving)(), )
    try:
        await asyncio.gather(consumer)
    finally:
        consumer.cancel()
        room.member_leave(obj)
示例#6
0
文件: state.py 项目: agoose77/pysam
 def wrapper(*args, **kwargs):
     coro = copy_current_websocket_context(coro_fun)(*args, **kwargs)
     asyncio.ensure_future(coro)