【发布时间】:2019-01-07 00:24:45
【问题描述】:
我正在开发一个包含聊天室的聊天应用程序。每个房间有两个用户。一个用户可以在多个房间中,即一个用户有多个房间。现在他在一个房间里聊天。但他在另一个房间收到一条消息。我想将其他房间的消息通知给用户。我应该如何实施?
目前一个websocket连接建立为:ws://localhost:8000/chat/int<room_id>/
group_name 被命名为"room"+room_id。到目前为止我有:
async def connect(self):
room_id = self.scope['url_route']['kwargs']['room_id']
await self.channel_layer.group_add(
"room"+room_id,
self.channel_name
)
await self.accept()
async def receive(self, text_data):
await self.channel_layer.group_send(
self.room_name,
{
'type': 'chat_message',
'message': json.loads(text_data)
}
)
async def chat_message(self, event):
await self.send(text_data=json.dumps({
'message': event['message']
}))
Django 2.x django 频道 2.x 蟒蛇3.6
【问题讨论】:
标签: django django-models django-views django-channels