django频道组发送突然不工作
我最近将我现有的django项目转换为react前端。然而,我面临的问题是,我的django频道的后端无法工作。我遇到的问题是,group_send方法在我的情况下不起作用,因此消费者无法接收到我的模型中信号生成的信息。这是我的密码: 消费者.py 型号/信号django频道组发送突然不工作,django,django-channels,consumer,Django,Django Channels,Consumer,我最近将我现有的django项目转换为react前端。然而,我面临的问题是,我的django频道的后端无法工作。我遇到的问题是,group_send方法在我的情况下不起作用,因此消费者无法接收到我的模型中信号生成的信息。这是我的密码: 消费者.py 型号/信号 每当在数据库中创建通知对象时,信号将发送到myChannelNotification,my接收此信息并通过group send将其发送给消费者。它曾经工作得很好,但我不确定在我将我的项目转换为react作为前端后发生了什么。似乎缩进有问题
每当在数据库中创建通知对象时,信号将发送到my
ChannelNotification
,my接收此信息并通过group send将其发送给消费者。它曾经工作得很好,但我不确定在我将我的项目转换为react作为前端后发生了什么。似乎缩进有问题,请纠正。似乎缩进有问题,请纠正。
class NotificationConsumer (AsyncJsonWebsocketConsumer):
async def connect (self):
close_old_connections()
user = self.scope["user"]
if not user.is_anonymous:
await self.accept()
#connects the user to his/her websocket channel
group_name = "notifications_{}".format(str(user))
print(group_name)
await self.channel_layer.group_add(group_name, self.channel_name)
async def disconnect (self, code):
close_old_connections()
user = self.scope["user"]
if not user.is_anonymous:
#Notifications
notifications_group_name = "notifications_{}".format(str(user))
await self.channel_layer.group_discard(notifications_group_name, self.channel_name)
async def user_notification (self, event):
close_old_connections()
print('Notification recieved by consumer')
await self.send_json({
'event': 'notification',
'data': {
'event_type': 'notification',
'notification_pk': event['notification_pk'],
'link': event['link'],
'date_created': event['date_created'],
'object_preview': event['object_preview'],
}
})
print(event)
def ChannelNotification(sender, instance, created, **kwargs):
if created:
channel_layer = get_channel_layer()
print(channel_layer)
group_name = "notifications_{}".format(str(instance.target))
print('inside channel signal')
print(group_name)
async_to_sync(channel_layer.group_send)(
group_name, {
"type": "user_notification",
"notification_pk": instance.pk,
"link": instance.object_url,
"date_created": instance.time.strftime("%Y-%m-%d %H:%M:%S"),
"object_preview": instance.object_preview,
}
)