简体   繁体   English

Django频道websocket.receive未处理

[英]Django channels websocket.receive is not handled

I'm trying to implement Django channels going through the docs. 我正在尝试通过文档实现Django渠道。
So like the docs i'm making consumers.py 因此,就像文档一样,我正在成为consumers.py

def ws_message(message):
    message.reply_channel.send({
        "text": message.content['text'],
})

and routing.py as routing.py作为

from channels.routing import route
from my_proj.consumers import ws_message

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

In my settings file I added channel_layers 在我的settings文件中,我添加了channel_layers

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgi_redis.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("localhost", 6379)],
        },
        "ROUTING": "my_proj.routing.channel_routing",
    },
}

So when I runserver and in chrome console send the following 所以当我运行服务器并在chrome控制台中发送以下内容

socket = new WebSocket("ws://" + 192.168.4.177:8000");
socket.onmessage = function(e) {
    alert(e.data);
}
socket.onopen = function() {
    socket.send("something");
}

I can see in manage.py console that Websocket connect worked and the connection is established, but the receive part is not handled and is not seen in console so alert from js code is not raised. 我可以在manage.py控制台中看到Websocket连接正常工作,并且建立了连接,但是未处理receive部分,并且在控制台中未看到receive部分,因此不会引发来自js代码的警报。 So what am I doing wrong? 那我在做什么错?

The problem was with the version of Twisted. 问题出在Twisted版本上。 By now the latest version of it is 16.3.0 but the Channels require 16.2.0 version. 到目前为止,它的最新版本是16.3.0,但是频道需要16.2.0版本。 So with 16.2.0 version of Twisted it works as should. 因此,对于Twisted的16.2.0版本,它可以正常工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM