Django-channels是否可以发送消息并等待响应?

时间:2020-10-15 19:25:39

标签: python django django-channels

在django-channels测试模块中有一种非常方便的发送消息并等待向后消息的方法:

from channels.testing import WebsocketCommunicator

communicator = WebsocketCommunicator(app, url)
connected, subprotocol = await communicator.connect()
await communicator.send_json_to(data=my_data)
resp = await communicator.receive_json_from(timeout=my_timeout)

是否可以用相同的样式编写真实消费者的代码?我的意思是这样的:

class MyConsumer(AsyncJsonWebsocketConsumer):
    async def some_method():
        await self.send_json(some_json)
        response = await self.receive_json(timeout=some_timeout)
        # do something with response

0 个答案:

没有答案