如何使用多聊天示例将成员添加到 Django Channels 2.1.1 中的同一个聊天室
How to add members to same chat room in Django Channels 2.1.1 using multichat example
我正在使用 Django Channels 2.1.1 和 Django 2.0 构建一个网站,客户按下一个按钮,就会被带到一个只有一名员工的聊天室。我从 Andrew Godwin 的 multichat 示例开始。
简而言之:我不知道如何正确地将员工添加到组中,以便他可以在聊天室中接收和发送消息。
我已阅读有关层、组和频道的 documentation。我不能说我对这些概念非常清楚,但我认为频道与个人用户有关,群组与聊天室或用户以某种方式组合在一起的等效物有关,层是沟通的媒介发生。
根据这种理解,我能做的最好的事情就是像这样添加到 join_room(self, room_id)
函数,这可能是完全错误的做法:
async def join_room_client(self, room_id):
...
# Added to find all staff who are logged in
authenticated_staff = get_all_logged_in_users()
# Added to list staff who are in less than 3 chatrooms
available_staff = []
if len(authenticated_staff) > 0:
for staff in authenticated_staff:
if staff in users_and_rooms and len(users_and_rooms[staff]) in range(3):
available_staff.append(staff)
random_available_staff = random.choice(available_staff)
# I thought receive_json() would look for "content", but I was wrong
content = {
"command": "join",
"join": room_id,
"type": "chat.join",
"room_id": room_id,
"text": "Hello there!",
"username": random_available_staff,
"title": room.title,
}
# Helps to open room in conjunction with chat_join()?
from channels.layers import get_channel_layer
channel_layer = get_channel_layer()
await channel_layer.send(users_and_channels[random_available_staff], content)
# This allows staff to receive messages in the chatroom but not to reply.
await self.channel_layer.group_add(
room.group_name,
# The line below is staff's channel name, eg: specific.PhCvZeuI!aCOgcyvgDanT
# I got it by accessing self.channel_name in connect(self)
users_and_channels[random_available_staff],
)
else:
print("Staff are not yet ready.")
else:
print("There are no staff available.")
使用此代码,房间会自动在员工的浏览器上打开。他可以看到客户的消息,但尝试回复会引发 "ROOM_ACCESS_DENIED"
,这意味着他尚未被添加到房间。但我想我已经用 group.add(room.group_name, users_and_channels[random_available_staff])
.
做到了
无论如何,大概最有效的方法是通过以下方式将有关聊天室的数据发送到工作人员的 receive_json(self, content)
when customer presses the button, right? But the function receives data from the Javascript in the template 实例:
socket.send(JSON.stringify({
"command": "join",
"room": roomId
})
如果可能,我如何使用 consumers.py 本身的消费者将有关客户聊天室的数据发送到员工 receive_json()
的聊天室?如果没有,我应该使用哪些其他方法?
原文join_room(self, room_id)
包含这两行关键:
# Store that we're in the room
self.rooms.add(room_id)
问题是,我不知道该把这条线放在哪里,这样它就可以添加员工而不是客户。经过大量试验(和粗心大意)后,我发现最好的放置位置是 chat_join()
:
async def chat_join(self, event):
...
# Right at the end
self.rooms.add(event["room_id"])
因为 chat_join()
在 join_room()
之后发挥作用,它让我们可以在他自己的循环中而不是在客户的循环中添加员工。
但是,如果这就是您将用户添加到房间的方式,那么这有什么作用:
await self.channel_layer.group_add(
room.group_name,
self.channel_name,
)
根据documentation,我还以为一个房间就是一组。我想我误读或误解了它。我又回去看了文档,但我仍然不知道两者之间有什么区别。
我正在使用 Django Channels 2.1.1 和 Django 2.0 构建一个网站,客户按下一个按钮,就会被带到一个只有一名员工的聊天室。我从 Andrew Godwin 的 multichat 示例开始。
简而言之:我不知道如何正确地将员工添加到组中,以便他可以在聊天室中接收和发送消息。
我已阅读有关层、组和频道的 documentation。我不能说我对这些概念非常清楚,但我认为频道与个人用户有关,群组与聊天室或用户以某种方式组合在一起的等效物有关,层是沟通的媒介发生。
根据这种理解,我能做的最好的事情就是像这样添加到 join_room(self, room_id)
函数,这可能是完全错误的做法:
async def join_room_client(self, room_id):
...
# Added to find all staff who are logged in
authenticated_staff = get_all_logged_in_users()
# Added to list staff who are in less than 3 chatrooms
available_staff = []
if len(authenticated_staff) > 0:
for staff in authenticated_staff:
if staff in users_and_rooms and len(users_and_rooms[staff]) in range(3):
available_staff.append(staff)
random_available_staff = random.choice(available_staff)
# I thought receive_json() would look for "content", but I was wrong
content = {
"command": "join",
"join": room_id,
"type": "chat.join",
"room_id": room_id,
"text": "Hello there!",
"username": random_available_staff,
"title": room.title,
}
# Helps to open room in conjunction with chat_join()?
from channels.layers import get_channel_layer
channel_layer = get_channel_layer()
await channel_layer.send(users_and_channels[random_available_staff], content)
# This allows staff to receive messages in the chatroom but not to reply.
await self.channel_layer.group_add(
room.group_name,
# The line below is staff's channel name, eg: specific.PhCvZeuI!aCOgcyvgDanT
# I got it by accessing self.channel_name in connect(self)
users_and_channels[random_available_staff],
)
else:
print("Staff are not yet ready.")
else:
print("There are no staff available.")
使用此代码,房间会自动在员工的浏览器上打开。他可以看到客户的消息,但尝试回复会引发 "ROOM_ACCESS_DENIED"
,这意味着他尚未被添加到房间。但我想我已经用 group.add(room.group_name, users_and_channels[random_available_staff])
.
无论如何,大概最有效的方法是通过以下方式将有关聊天室的数据发送到工作人员的 receive_json(self, content)
when customer presses the button, right? But the function receives data from the Javascript in the template 实例:
socket.send(JSON.stringify({
"command": "join",
"room": roomId
})
如果可能,我如何使用 consumers.py 本身的消费者将有关客户聊天室的数据发送到员工 receive_json()
的聊天室?如果没有,我应该使用哪些其他方法?
原文join_room(self, room_id)
包含这两行关键:
# Store that we're in the room
self.rooms.add(room_id)
问题是,我不知道该把这条线放在哪里,这样它就可以添加员工而不是客户。经过大量试验(和粗心大意)后,我发现最好的放置位置是 chat_join()
:
async def chat_join(self, event):
...
# Right at the end
self.rooms.add(event["room_id"])
因为 chat_join()
在 join_room()
之后发挥作用,它让我们可以在他自己的循环中而不是在客户的循环中添加员工。
但是,如果这就是您将用户添加到房间的方式,那么这有什么作用:
await self.channel_layer.group_add(
room.group_name,
self.channel_name,
)
根据documentation,我还以为一个房间就是一组。我想我误读或误解了它。我又回去看了文档,但我仍然不知道两者之间有什么区别。