Django 频道没有收到消息
Django channel not getting message
我在当前项目中使用 Django 通道。从我的一个 django 应用程序,我正在向通道层发送通知,以便 websocket 可以广播消息。但问题是
消费者没有收到我的信息。
django 应用程序中用于向频道发送通知的实用程序:
from asgiref.sync import AsyncToSync
from channels.layers import get_channel_layer
import json
def async_send(group_name, text):
channel_layer = get_channel_layer()
AsyncToSync(channel_layer.group_send)(
group_name,
{
'type': 'notify',
'text': json.dumps(text)
}
)
我的消费者档案是:
from channels.generic.websocket import AsyncWebsocketConsumer
class InformationConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.channel_layer.group_add(str(self.scope['user']), self.channel_name)
await self.accept()
async def notify(self, event):
await self.send(
{
"message": event['text'],
},
)
print(event['text'])
我应该得到事件['text']的输出,但什么也没得到:(
从
更改
self.channel_layer.group_add(str(self.scope['user']), self.channel_name)
至
await self.channel_layer.group_add(str(self.scope['user']), self.channel_name)
我在当前项目中使用 Django 通道。从我的一个 django 应用程序,我正在向通道层发送通知,以便 websocket 可以广播消息。但问题是 消费者没有收到我的信息。
django 应用程序中用于向频道发送通知的实用程序:
from asgiref.sync import AsyncToSync
from channels.layers import get_channel_layer
import json
def async_send(group_name, text):
channel_layer = get_channel_layer()
AsyncToSync(channel_layer.group_send)(
group_name,
{
'type': 'notify',
'text': json.dumps(text)
}
)
我的消费者档案是:
from channels.generic.websocket import AsyncWebsocketConsumer
class InformationConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.channel_layer.group_add(str(self.scope['user']), self.channel_name)
await self.accept()
async def notify(self, event):
await self.send(
{
"message": event['text'],
},
)
print(event['text'])
我应该得到事件['text']的输出,但什么也没得到:(
从
更改self.channel_layer.group_add(str(self.scope['user']), self.channel_name)
至
await self.channel_layer.group_add(str(self.scope['user']), self.channel_name)