Django 频道用户名发件人

Django channels username sender

我是 Django 频道的新手,我按照文档教程制作了一个 Django 聊天室,到目前为止我可以发送和接收消息,但是这里的问题是发件人未知,我尝试发送用户名但是它不起作用,我在服务器上打印了用户名,但在前面我得到了实际的登录用户名??我很困惑?

consumer.py :

class ChatConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = 'chat_%s' % self.room_name
        self.user = self.scope["user"]
        # Join room group
        await self.channel_layer.group_add(
            self.room_group_name,
            self.channel_name
        )

        await self.accept()

    async def disconnect(self, close_code):
        # Leave room group
        await self.channel_layer.group_discard(
            self.room_group_name,
            self.channel_name
        )

    # Receive message from WebSocket
    async def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']

        # Send message to room group
        await self.channel_layer.group_send(
            self.room_group_name,
            {
                'type': 'chat_message',
                'message': message
            }
        )

    # Receive message from room group
    async def chat_message(self, event):
        message = event['message']
        print(self.user.username)

        # Send message to WebSocket
        await self.send(text_data=json.dumps({
            'message': message,
            'username' : str(self.user.username),
        }))

room.html :

<!-- chat/templates/chat/room.html -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Chat Room</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<div style="width:800px; margin:0 auto;">username :
     <a  id="username" href="">{{ user.get_username }}</a>
     <textarea id="chat-log" cols="100" rows="20"></textarea><br/>
    <input id="chat-message-input" type="text" size="75"/><br/>
    <input id="chat-message-submit" type="button" value="Send"/>
</div>

</body>
<script>
    var roomName = {{ room_name_json }};

    var chatSocket = new WebSocket(
        'ws://' + window.location.host +
        '/ws/chat/' + roomName + '/');

    chatSocket.onmessage = function(e) {
        var data = JSON.parse(e.data);
        var message = data['message'];
        console.log(e.data)

        document.querySelector('#chat-log').value += ( message + '\n');
    };

    chatSocket.onclose = function(e) {
        console.error('Chat socket closed unexpectedly');
    };

    document.querySelector('#chat-message-input').focus();
    document.querySelector('#chat-message-input').onkeyup = function(e) {
        if (e.keyCode === 13) {  // enter, return
            document.querySelector('#chat-message-submit').click();
        }
    };

    document.querySelector('#chat-message-submit').onclick = function(e) {
        var messageInputDom = document.querySelector('#chat-message-input');
        var message = messageInputDom.value;
        chatSocket.send(JSON.stringify({
            'message': message
        }));

        messageInputDom.value = '';
    };
</script>
</html>

当用户连接时,他会得到一个单独的 ChatConsumer 实例,而您设置 self.user。如果他从另一个频道收到消息,self.user不会改变,仍然是你给他设置的那个。所以要显示发件人的用户名,您必须像这样将其与消息一起发送:

    ...
    # Receive message from WebSocket
    async def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']

        # Send message to room group
        await self.channel_layer.group_send(
            self.room_group_name,
            {
                'type': 'chat_message',
                'message': message,
                'username': self.user.username
            }
        )

    # Receive message from room group
    async def chat_message(self, event):
        message = event['message']
        username = event['username']
        print(username)

        # Send message to WebSocket
        await self.send(text_data=json.dumps({
            'message': message,
            'username' : username,
        }))

重要的是要注意 ChatConsumer 运行 方法 receivechat_message 的单独实例,因此这些方法中的 self.user 是不同的。 receive 方法在发送者的频道中执行,而 chat_message 方法在接收者的频道中执行,在这种情况下,发送者也将包括发送者,因为他与接收者在同一组中. 例如,要查看您收到的消息是否是您发送的,您可以在 chat_message 方法中执行以下操作:

if event['username'] == self.user.username:
    ...

我正在处理同样的问题并测试了对我有用的解决方案。我在邮件中附加了 logged-in 用户名。

# Receive message from WebSocket
async def receive(self, text_data):
        
    username = self.scope["user"].username # store logged-in user from WebSocket scope
    text_data_json = json.loads(text_data)
    message = text_data_json['message']
    message = (username + ': ' + message) # add to message here
    
    # Send message to room group
    await self.channel_layer.group_send(
        self.room_group_name,
        {
            'type': 'chat_message',
            'message': message,
        }
    )

# Receive message from room group
async def chat_message(self, event):
    
    username = self.scope["user"].username
    message = event['message']
    
    # Send message to WebSocket
    await self.send(text_data=json.dumps({
        'message': message,
        "username": username,
    }))