python - Django Channels:应用程序内部异常:消息类型app_consumer.notification_message没有处理程序
问题描述
我正在尝试在特定事件上使用 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
}))
但我收到以下错误消息:
应用程序内部异常:没有消息类型 app_consumer.notification_message 的处理程序
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/channels/consumer.py”,第 59 行,调用 [receive,self.channel_receive],self.dispatch
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/channels/utils.py”,第 51 行,在 await_many_dispatch 等待调度(结果)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/asgiref/sync.py”,第 244 行,调用中 返回等待 asyncio.wait_for(future, timeout=None)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/tasks.py”,第414行,在wait_for return await fut
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/thread.py”,第 57 行,运行结果 = self.fn(*self.args, **self.夸格斯)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/channels/db.py”,第 14 行,在 thread_handler return super().thread_handler(loop, *args, * *夸格斯)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/asgiref/sync.py”,第 277 行,在 thread_handler return func(*args, **kwargs)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/channels/consumer.py”,第 107 行,在 dispatch raise ValueError("No handler for message type %s" %消息[“类型”])
没有消息类型 app_consumer.notification_message 的处理程序
解决方案
您在这里发出的是您的notification_message
处理程序方法的名称。
Channels 期望它被命名为app_consumer_notification_message
.
Channels在查找消息处理方法时将.
in 消息类型替换为。_
(消息类型中的前缀不用于仅路由组名称)
推荐阅读
- android - Firebase 与 Cordova:选择哪个平台
- node.js - Dockercontainer 中的 dns 无法与 vue.js 和节点一起使用:错误:getaddrinfo ENOTFOUND db
- gatling - Gatling FrontLine Reporting_能够查看响应时间趋势
- web-development-server - 使用的 Localstack 限制服务(因此希望在内存上更容易)
- .net - 在 IIS 上的 InProcess 中托管 .net 核心 Web api 时,app.UseSwagger() 失败
- python - 从非结构化医疗文档中提取文本用于 NLP
- architecture - CQRS:查询和业务规则
- php - 为什么这个采用平面数组的函数需要这么长时间才能使其成为嵌套数组?
- amazon-web-services - CloudFormation - 手动删除堆栈中的资源并更新
- flutter - 在小部件构建期间调用的 setState()