【发布时间】:2021-02-28 00:39:57
【问题描述】:
我使用 django 频道 3.0.0 和使用 angular 的 websocket。 但是用户连接到django websocket,他们分别在自己的房间里。 当我想将事件发送给消费者之外的连接用户时,我使用了以下代码。
all_users = list(Member.objects.filter(is_active = True).values_list('id', flat = True))
for user_id in all_users:
async_to_sync(channel_layer.group_send)(
"chat_{}".format(user_id),
{ "type": "tweet_send", "message": "tweet created" }
)
在consumers.py 中,我的消费者类的chat_message 函数
async def tweet_send(self, event):
content = event['message']
# Send message to WebSocket
await self.send(text_data=json.dumps({
"type": "MESSAGE",
"data": content
}))
而这个“self.send”函数是分别发送给所有连接的用户的,但是当我运行代码时,所有的数据只发送给上次连接的一个用户。 我不知道为什么。如果有人知道原因,请帮助我。
【问题讨论】:
-
django-channels已在3.0.1和3.0.2中发布了错误修复。我建议尝试使用版本3.0.2。如果问题仍然存在,请尝试禁用django-debug-toolbar(如果您的 INSTALLED_APPS 中有它),它往往会从 django-channels 中吸取异常,从而阻止它们在运行服务器日志时被打印。如果问题仍然存在,我会在 django-channels 中提交错误报告。 -
感谢您的回答。正如你告诉我的,我已经修复了错误。
标签: django websocket django-channels