Django频道 - 回声示例不工作 [英] Django channels - Echo example not working

查看:224
本文介绍了Django频道 - 回声示例不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循



请求我的状态为待处理(尽管我在此阅读了



而服务器输出如下所示:





每次我我试图通过websocket发送东西在浏览器中,服务器只是打印CONNECT,但没有显示js控制台的日志。



编辑:我已经在浏览器中使用echo.websocket.org测试了Websockets,并按预期得到了答案:



解决方案

我改为旧版本的扭曲修复。 Hth


I'm following the instructions in the documentation site, but I got stuck in the echo example, the websocket is created correctly and it's connected to the server but when I send anything to the server I'm not getting any response (In the example says I should see an alert window with the same message that I send into the socket but I don't, although I've changed the alert for a console.log but still), what I'm doing wrong?

In settings.py:

INSTALLED_APPS = {
    ...
    'channels',
    'myapp',
    ...
} 

...
# Channels settings
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgiref.inmemory.ChannelLayer",
        "ROUTING": "myapp.routing.channel_routing",
    },
}

In routing.py:

from channels.routing import route
from myapp.consumers import *

channel_routing = [
    route("websocket.receive", ws_receive),
]

In consumers.py:

def ws_receive(message):
    # ASGI WebSocket packet-received and send-packet message types
    # both have a "text" key for their textual data.
    message.reply_channel.send({
        "text": message.content['text'],
    })

In asgi.py

import os
from channels.asgi import get_channel_layer

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")

channel_layer = get_channel_layer()

Then I run: python manage.py runserver, and in my browser I go to the server url and in the console I put the following:

socket = new WebSocket("ws://" + window.location.host + "/chat/");
socket.onmessage = function(e) {
    alert(e.data);
}
socket.onopen = function() {
    socket.send("hello world");
}

Again, at this point I should see an alert window (or the console.log message) but I get nothing.

The requests that I made have a status of pending (Although I read here and the first comment says it's normal)

And the server output looks like this:

Every time that I've tried to send something through the websocket in the browser, the server just print CONNECT but no log from the js console is showing.

Edit: I've tested websockets in my browser against echo.websocket.org and I got the answer as expected:

解决方案

I changed to an older version of twisted and it fixed it. Hth

这篇关于Django频道 - 回声示例不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆