【发布时间】:2019-08-14 02:17:45
【问题描述】:
我想使用 Channels 2 向特定的经过身份验证的用户发送通知。
在下面的代码中,我将通知作为广播发送,而不是我想向特定用户发送通知。
from channels.generic.websocket import AsyncJsonWebsocketConsumer
class NotifyConsumer(AsyncJsonWebsocketConsumer):
async def connect(self):
await self.accept()
await self.channel_layer.group_add("gossip", self.channel_name)
print(f"Added {self.channel_name} channel to gossip")
async def disconnect(self, close_code):
await self.channel_layer.group_discard("gossip", self.channel_name)
print(f"Removed {self.channel_name} channel to gossip")
async def user_gossip(self, event):
await self.send_json(event)
print(f"Got message {event} at {self.channel_name}")
【问题讨论】:
标签: python django python-3.x websocket django-channels