如何使用multichat示例向Django Channel 2.1.1中的同一聊天室添加成员

如何使用multichat示例向Django Channel 2.1.1中的同一聊天室添加成员,django,django-channels,Django,Django Channels,使用Django频道2.1.1和Django频道2.0,我正在构建一个网站,在这个网站上,一位客户按下按钮,然后被带进一个聊天室,里面有一名工作人员。我从安德鲁·戈德温的例子开始 简言之:我不知道如何正确地将工作人员添加到组中,以便他可以在聊天室中接收和发送消息 我读过关于图层、组和频道的文章。我不能说我对这些概念非常清楚,但我认为频道属于个人用户,群组属于聊天室或类似的聊天室,用户以某种方式分组在一起,层是进行通信的媒介 根据这种理解,我所能做的最好的事情就是向函数中添加这样的内容,这可能是完

使用Django频道2.1.1和Django频道2.0,我正在构建一个网站,在这个网站上,一位客户按下按钮,然后被带进一个聊天室,里面有一名工作人员。我从安德鲁·戈德温的例子开始

简言之:我不知道如何正确地将工作人员添加到组中,以便他可以在聊天室中接收和发送消息

我读过关于图层、组和频道的文章。我不能说我对这些概念非常清楚,但我认为频道属于个人用户,群组属于聊天室或类似的聊天室,用户以某种方式分组在一起,层是进行通信的媒介

根据这种理解,我所能做的最好的事情就是向函数中添加这样的内容,这可能是完全错误的:

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.")
使用此代码,房间将在员工浏览器上自动打开。他可以看到客户的消息,但试图回复会引发“房间访问被拒绝”,这意味着他没有被添加到房间中。但我想我已经用
group.add(room.group\u name,users\u和\u channels[random\u available\u staff])做到了这一点。

无论如何,最有效的方法大概是当客户按下按钮时,将聊天室的数据发送到工作人员的实例中,对吗?但函数通过以下方式从中的Javascript接收数据:

socket.send(JSON.stringify({
                        "command": "join",
                        "room": roomId
                    })
如果可能,我如何使用consumers.py中的consumers将有关客户聊天室的数据发送到员工的
receive_json()
?如果没有,我应该使用什么其他方法?

原文包含以下两行关键内容:

# Store that we're in the room
self.rooms.add(room_id)
问题是,我不知道该把这条线放在哪里,这样它就可以增加员工而不是客户。经过大量的实验(和粗心大意),我发现最好的放置位置是:

因为
chat\u join()
join\u room()
之后开始发挥作用,所以我们可以在他自己的循环中而不是在客户的循环中添加员工

但是,如果这就是向文件室添加用户的方式,那么这会起什么作用:

    await self.channel_layer.group_add(
        room.group_name,
        self.channel_name,
    )
基于这一点,我认为房间是一个群体。我想我误解了它。我又去看了一遍文档,但我仍然不知道两者之间有什么区别

    await self.channel_layer.group_add(
        room.group_name,
        self.channel_name,
    )