async def one_to_one(request, receiver): async for message in receiver: if message == "hello": yield ws_send("hello beautiful") elif message == "exit": yield ws_send("byebye") break elif message == "i like you": yield ws_send("That is very nice! I like you too!") else: yield ws_send("pardon me. I do not have a reply to this")
async def chatroom(scope, receiver, name): client_msgs = named_receiver(name, receiver) broadcast_messages = receive_from_broadcast(name) async for user_name, message in merge(client_msgs, broadcast_messages): if user_name == name: send_broadcast(name, message) else: yield ws_send(f"@{user_name}: {message}")
async def test_ws_handler_raising_ws_disconnect_does_not_leak_outside(): sended_messages = await run_ws_handler_with_messages( raise_ws_disconnect_simulate_client_disconnect, [ { "type": "websocket.connect" }, ws_receive("mb:lisa"), ], ) assert sended_messages == [ call({"type": "websocket.accept"}), call(ws_send("this is good")) ]
async def test_close_gets_automatically_sended_when_client_stops_yielding_messages( ): async def ws_close_immediately(scope, receiver): yield ws_send("adf") sender = Mock().send async_sender = awaitable_mock(sender) fake_messages = list(map(ws_receive, ["a"])) receiver = receive_func_from_coll(fake_messages) await _ws_async_generator_client(ws_close_immediately, {}, tuple(), receiver, async_sender) assert sender.call_args_list == list( map(call, [ws_send("adf"), { "type": "websocket.close", "code": 1000 }]))
async def fan_out(request, receiver): while True: yield (ws_send(f"current-time-stamp {time.time()}")) await asyncio.sleep(1)
async def echo_server(request, receiver): async for message in receiver: yield ws_send(f"@echo: {message}")
async def raise_ws_disconnect_simulate_client_disconnect(scope, receive): yield ws_send("this is good") raise WSDisconnect("Good Bye")
async def send_2_messages(scope, receive): for index in range(2): yield ws_send(str(index))
async def ws_close_immediately(scope, receiver): yield ws_send("adf")