gpt4 book ai didi

python - Django channel : Exception inside application: No handler for message type app_consumer. notification_message

转载 作者:太空宇宙 更新时间:2023-11-03 19:43:23 25 4
gpt4 key购买 nike

我正在尝试使用 websockets 在特定事件上向用户发送通知。我有以下模型接收器,它触发 websocket 方法:

@receiver(pre_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, update_fields=None, **kwargs):
if update_fields:
if 'pan_number_verified' in update_fields and instance.pan_number_verified is True:
notification = Notification.objects.create(message='Your PAN Number has been verified...Head on to '
'creating a campaign right away.', user=instance)
channel_layer = get_channel_layer()
async_to_sync(channel_layer.group_send)("notification_%s" % instance.id, {
"type": "app_consumer.notification_message",
"message": str(notification.id)
})

这是我的应用程序使用者:

class AppConsumer(WebsocketConsumer):
def connect(self):
self.room_name = self.scope['url_route']['kwargs']['user_id']
self.room_group_name = 'notification_%s' % self.room_name
async_to_sync(self.channel_layer.group_add)(
self.room_group_name,
self.channel_name
)
self.accept()

def disconnect(self, close_code):
async_to_sync(self.channel_layer.group_discard)(
self.room_group_name,
self.channel_name
)

def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json['message']

async_to_sync(self.channel_layer.group_send)(
self.room_group_name,
{
'type': 'app_consumer.notification_message',
'message': message
}
)

def notification_message(self, event):
message = event['message']

self.send(text_data=json.dumps({
'message': message
}))

但我收到以下错误消息:

Exception inside application: No handler for message type app_consumer.notification_message

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/channels/consumer.py", line 59, in call [receive, self.channel_receive], self.dispatch

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/channels/utils.py", line 51, in await_many_dispatch await dispatch(result)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/asgiref/sync.py", line 244, in call return await asyncio.wait_for(future, timeout=None)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/tasks.py", line 414, in wait_for return await fut

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/thread.py", line 57, in run result = self.fn(*self.args, **self.kwargs)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/channels/db.py", line 14, in thread_handler return super().thread_handler(loop, *args, **kwargs)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/asgiref/sync.py", line 277, in thread_handler return func(*args, **kwargs)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/channels/consumer.py", line 107, in dispatch raise ValueError("No handler for message type %s" % message["type"])

No handler for message type app_consumer.notification_message

最佳答案

您在此处发出的是 notification_message 处理程序方法的名称。

channel 希望将其命名为 app_consumer_notification_message

在查找消息处理方法时,Channels 将消息类型中的 . 替换为 _

(消息类型中的前缀不用于仅路由组名称)

关于python - Django channel : Exception inside application: No handler for message type app_consumer. notification_message,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60298407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com