django channels integration issue, websocket.receive not listening

与世无争的帅哥 提交于 2019-12-12 02:38:49

问题


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:


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



来源:https://stackoverflow.com/questions/40618438/django-channels-integration-issue-websocket-receive-not-listening

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!