2017-07-26 29 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

希望它可以幫助和我說,我會作出評論,但我不能,因爲我代表的顯然...