2017-07-26 86 views
0

我正在研究一个django项目,其中涉及不同应用程序的频道。第一个应用程序(从传感器接收数据)具有它自己的消费者和路由,以及第二个应用程序(更新已登录用户的列表)。Django频道 - 握手和连接,但websocket.connect函数未执行

在第一个应用程序内一切正常。

在第二个应用程序中握手完成并建立连接,但链接到websocket.receive的功能未执行。

from channels.routing import route, route_class 
from channels.staticfiles import StaticFilesConsumer 
from users.consumer import ws_connect, ws_disconnect 

channel_routing = [ 
    route('websocket.connect', ws_connect, path=r'^/users/lobby/'), 
    ... 
] 

ws_connect

import json 
from channels import Group 
from channels.handler import AsgiHandler 
from channels.auth import channel_session_user, 
    channel_session_user_from_http, channel_session 

@channel_session_user_from_http 
def ws_connect(message): 
    print('test') 

的的ws_connectprint('test')从不执行。此外,它甚至不关心我在JavaScript中使用什么网址结束。

var ws = new WebSocket('ws://127.0.0.1:8000/users/lobby/'); 

ws.onopen = function() { 
    console.log('connect'); 
    ws.send('connect'); 
} 

的JavaScript的ws将与.../users/lobby/.../users/.../lobby/连接。

感谢您的任何提示!

回答

0

因为我显然需要50个声望才能发表评论我只是想作出一个答案,而不是我不知道这是你的解决方案。

复制部分由制作频道的人制作的github多频道项目时,我遇到了同样的问题。

我的问题显然是我hade miss拼写错误,因此它没有调用我为该路由设置的正确函数。

所以我的建议是,你trippl检查到处的路由,看看你是否搞砸了。我个人只在错误检查时检查了我的APP路由,但它在我的项目文件夹中,我弄错了我的路由。

另一件可能引发此问题的事情是,您的应用只有一个路由,但不在主文件夹中。在这种情况下,你需要包括其他应用程序与你想为它就像一个视图的路径路由:

from channels import include 


channel_routing = [ 
     include("crypto_chat.routing.websocket_routing", path=r"^/chat/stream/$"), 
     include("crypto_chat.routing.chat_routing"), 
] 

应用路由:

from channels import route 
from .consumers import ws_connect, ws_receive, ws_disconnect, user_offline, user_online, chat_send 

websocket_routing = [ 
    route("websocket.connect", ws_connect), 
    route("websocket.receive", ws_receive), 
    route("websocket.disconnect", ws_disconnect), 
] 

chat_routing = [ 
    route("chat.receive", chat_send, command="^send$"), 
    route("chat.receive", user_online, command="^online$"), 
    route("chat.receive",user_offline, command="^offline$"), 
] 

如果那不帮助,那么我建议你头以上以github渠道页面为例,并比较两者。请记住,该项目可能是由一个更加频道的版本(也可能是django)制作的。

https://github.com/andrewgodwin/channels-examples

希望它可以帮助和我说,我会作出评论,但我不能,因为我代表的显然...