django频道整合问题,websocket.receive不收听 [英] django channels integration issue, websocket.receive not listening
问题描述
通过各种博客文章,我尝试使用django实现websockets功能的django渠道
Going through various blog post, I am trying to implement django channels for websockets functionality with django
我正在使用django 1.9.1
I am using django 1.9.1
具有以下一组依赖关系:
asgi-redis == 0.10.0
channel == 0.12.0
daphne == 0.11.1
with these set of dependencies: asgi-redis==0.10.0 channels==0.12.0 daphne==0.11.1
settings.py
settings.py
CHANNEL_LAYERS = {
"default": {
"BACKEND": "asgiref.inmemory.ChannelLayer",
"ROUTING": "test.routing.channel_routing",
},
}
routing.py
routing.py
from channels.routing import route
from .consumers import websocket_receive
channel_routing = [
route("websocket.receive", websocket_receive, path=r"^/chat/"),
]
Consumer.py
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");
}
在上述调用中,在运行服务器日志中,我可以看到对websocket的调用像这样: [2016/11/15 19:35:39] WebSocket CONNECT / chat / [127.0.0.1:55499],但从未调用我的consumers.py方法(websocket_receive)。
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 ??
推荐答案
降低Twisted的版本已解决问题,安装的Twisted的默认版本是16.5,使用16.2解决了。
Lowering the version of Twisted solved the issue, Default version of Twisted installed was 16.5, using 16.2 solved it.
这篇关于django频道整合问题,websocket.receive不收听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!