首页 > 解决方案 > 如何使用多聊天示例将成员添加到 Django Channels 2.1.1 中的同一聊天室

问题描述

使用 Django Channels 2.1.1 和 Django 2.0,我正在构建一个站点,客户在该站点中按下按钮并被带入一个有一名工作人员的聊天室。我从 Andrew Godwin 的多人聊天示例开始。

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

我已阅读有关层、组和通道的文档。我不能说我对这些概念非常清楚,但我认为频道与单个用户有关,群组与聊天室或用户以某种方式分组在一起的等价物有关,并且层是沟通的媒介发生。

使用这种理解,我能做的最好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.")

使用此代码,房间会自动在员工的浏览器上打开。他可以看到客户的消息,但试图回复 raises "ROOM_ACCESS_DENIED",这意味着他还没有被添加到房间。但我以为我已经做到了group.add(room.group_name, users_and_channels[random_available_staff])

无论如何,据推测,最有效的方法是将有关聊天室的数据发送到工作人员的receive_json(self, content)客户按下按钮时的实例,对吧?但是该函数通过以下方式从模板中的 Javascript 接收数据:

socket.send(JSON.stringify({
                        "command": "join",
                        "room": roomId
                    })

如果可能的话,我如何将有关客户聊天室的数据发送给工作人员receive_json()在消费者.py 本身中使用消费者?如果不是,我应该使用哪些其他方法?

标签: djangodjango-channels

解决方案


原文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,
    )

根据文档,我认为一个房间是一个组。我想我误读或误解了它。我又回去阅读文档,但我仍然不知道两者之间的区别是什么。


推荐阅读