har*_*sak 5 python django django-signals websocket django-channels
我想在保存模型时通知客户。我首先在 post_save 上创建了一个 django 信号。
@receiver(post_save, sender=Scooter)
async def scooter_post_update(sender, instance, created, **kwargs):
# Notify client here
Run Code Online (Sandbox Code Playgroud)
接下来,我从 django-channels 创建了 AsyncConsumer 类并提供了它的路由。
// routing.py
application = ProtocolTypeRouter({
# Empty for now (http->django views is added by default)
'websocket': AllowedHostsOriginValidator(
AuthMiddlewareStack(
URLRouter(
[
path('scooters/', ScootersUpdateConsumer)
]
)
)
)
})
// consumers.py
class ScootersUpdateConsumer(AsyncConsumer):
async def websocket_connect(self, event):
print("Connected!", event)
await self.send({
"type": "websocket.accept"
})
async def send_message(self):
await self.send({
"type": "websocket.send",
'text': 'Oy, mate!'
})
async def websocket_receive(self, event):
print("Receive!", event)
async def websocket_disconnect(self, event):
print("Disconnected!", event)
Run Code Online (Sandbox Code Playgroud)
现在我的问题是如何从 scooter_post_update() 方法调用 send_message() 。
步骤非常简单。您必须获取通道层并发送一条消息,并将type密钥设置为您的监听方法名称:
import channels
from asgiref.sync import async_to_sync
@receiver(post_save, sender=Scooter)
def scooter_post_update(sender, instance, created, **kwargs):
channel_layer = channels.layers.get_channel_layer()
async_to_sync(channel_layer.send)(
{"type": "send_message", "data": data}
)
Run Code Online (Sandbox Code Playgroud)
以及您想通过频道发送的任何其他内容。
请注意,您传递的所有数据都必须是可序列化的,因此您必须注意所有对象都已预先序列化。
传递给方法的字典的强制部分send是type键(如前所述),它必须包含将在使用者上调用的方法名称。
此外,您可以使用组,这样您就可以向一组侦听器广播消息:
import channels
from asgiref.sync import async_to_sync
@receiver(post_save, sender=Scooter)
def scooter_post_update(sender, instance, created, **kwargs):
channel_layer = channels.layers.get_channel_layer()
async_to_sync(channel_layer.group_send)(
"group_name", {"type": "send_message", "data": data}
)
Run Code Online (Sandbox Code Playgroud)
在消费者方面:
class ScootersUpdateConsumer(AsyncConsumer):
async def websocket_connect(self, event):
await self.channel_layer.group_add("group_name", self.channel_name)
await self.send({
"type": "websocket.accept"
})
Run Code Online (Sandbox Code Playgroud)
请注意,在这两种情况下,您都使用async_to_sync从同步范围调用异步代码时应使用的包装器。
| 归档时间: |
|
| 查看次数: |
1397 次 |
| 最近记录: |