django channels integration issue, websocket.recei

2019-09-11 12:22发布

Going through various blog post, I am trying to implement django channels for websockets functionality with django

I am using django 1.9.1

with these set of dependencies: asgi-redis==0.10.0 channels==0.12.0 daphne==0.11.1

settings.py

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgiref.inmemory.ChannelLayer",
        "ROUTING": "test.routing.channel_routing",
    },
}

routing.py

from channels.routing import route
from .consumers import websocket_receive

channel_routing = [
    route("websocket.receive", websocket_receive, path=r"^/chat/"),
]

consumers.py

def websocket_receive(message):
    text = message.content.get('text')
    if text:
        message.reply_channel.send({"text": "You said: {}".format(text)})

After runserver from browser console i am calling this

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

On above call, in runserver logs I can see a call for websocket, something like this: "[2016/11/15 19:35:39] WebSocket CONNECT /chat/ [127.0.0.1:55499]", but my consumers.py method(websocket_receive) is never called..

Any idea where I may be going wrong ??

1条回答
何必那么认真
2楼-- · 2019-09-11 12:50

Lowering the version of Twisted solved the issue, Default version of Twisted installed was 16.5, using 16.2 solved it.

查看更多
登录 后发表回答