- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 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/
我正在尝试使用 websockets 在特定事件上向用户发送通知。我有以下模型接收器,它触发 websocket 方法: @receiver(pre_save, sender=settings.AUT
我是一名优秀的程序员,十分优秀!