简体   繁体   English

Django-channels:chatSocket.onmessage 或 chatSocket.send 不起作用

[英]Django-channels:chatSocket.onmessage or chatSocket.send does not work

I'm trying to implement a django channels chat app.我正在尝试实现 django 频道聊天应用程序。 when the submit button of the room view is clicked, the message does not appear in the chat log.单击房间视图的提交按钮时,该消息不会出现在聊天日志中。 which make me think that somethings wrong with chat.onmessage command, it does not seem to fire.这让我觉得 chat.onmessage 命令有问题,它似乎没有触发。 can someone help me fix the issue.有人可以帮我解决这个问题。 here is the code for room view:这是房间视图的代码:

<!-- chat/templates/room.html -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Chat Room</title>
</head>
<body>
    <textarea id="chat-log" cols="100" rows="20"></textarea><br/>
    <input id="chat-message-input" type="text" size="100"/><br/>
    <button id="chat-message-submit" type="submit" value="Send">Send</button>
</body>z
<script>
    var roomName = {{ room_name_json }};
    var chatSocket = new WebSocket(
        'ws://' + window.location.host +
        '/ws/chat/' + roomName + '/');

    chatSocket.onmessage = function(e) {
        console.log("got to onmessage");
        var data = JSON.parse(e.data);
        var message = data['message'];
        document.getElementById('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.getElementById('chat-message-submit').click();
        }
    };

    document.getElementById('chat-message-submit').onclick = function(e) {
        var messageInputDom = document.getElementById('chat-message-input');
        var message = messageInputDom.value;
        console.log("got message : " + message);
        chatSocket.send(JSON.stringify({
            'message': message
        }));
        console.log("This was done?");
        messageInputDom.value = '';
    };
</script>
</html>

Here is my consumer view:这是我的消费者观点:

from channels.generic.websocket import WebsocketConsumer
import json

class ChatConsumer(WebsocketConsumer):
    def connect(self):
        self.accept()

    def disconnect(self, close_code):
        pass

    def recieve(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']
        self.send(text_data = json.dumps({
            'message' : message
        }))

I'm so stupid I literally made a typo, used function name "received" instead of "recieved".我太愚蠢了,我确实打错了字,使用 function 名称“received”而不是“received”。 Thanks I'll go cry in the corner now.谢谢,我现在会在角落里哭泣 go。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native JavaScript:TypeError:null 不是 object(评估“chatSocket.send”) - React Native JavaScript: TypeError: null is not an object (evaluating 'chatSocket.send') 如何使用 django-channels 2 处理消息? - How to process messages with django-channels 2? 找不到“ / lobby /” django-channels路由错误 - Not Found “/lobby/” django-channels routing error 将授权凭证发送到Django通道WebSocket(无需将令牌设置为cookie) - Send Authorization Credentials to Django-channels WebSocket (without setting token as cookie) WebSocket 连接失败 Django 通道 - WebSocket connection fails Django-channels django频道无法正常运作我已阅读文件 - django channels not working how does it work i have read docs 为什么使用函数声明不适用于网络工作者的 onmessage? - Why does using a function declaration not work for onmessage with web workers? 删除单个频道/所有频道均无效 - Deleting single channel/all channels does not work Chrome.runtime.onMessage无法将消息从弹出窗口发送到内容脚本 - Chrome.runtime.onMessage doesn't work to send message from popup to content script 为什么我的Chrome扩展程序内容脚本的运行时没有任何onMessage仅连接和发送消息 - Why does the runtime of my Chrome extension content script not have any onMessage only connect and send message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM