2017-01-24 66 views
2

我試圖用Django頻道與瀏覽器建立一個websocket連接。 WebSocket的無法連接服務器:用於WebSocket的使用Django頻道的Websocket

[2017/01/23 23:51:50] HTTP GET/200 [0.03, 127.0.0.1:60445] 
[2017/01/23 23:51:51] WebSocket HANDSHAKING /chat/ [127.0.0.1:60451] 
[2017/01/23 23:51:56] WebSocket DISCONNECT /chat/ [127.0.0.1:60451] 

的Javascript:

socket = new WebSocket("ws://" + window.location.host + "/chat/"); 
    socket.onmessage = function (e) { 
     alert(e.data); 
    }; 
    socket.onopen = function() { 
     socket.send("hello world"); 
    }; 
    // Call onopen directly if socket is already open 
    if (socket.readyState == WebSocket.OPEN) socket.onopen(); 

settings.py

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

Routing.py

channel_routing = { 
    # Wire up websocket channels to our consumers: 
    'websocket.connect': ws_add, 
    'websocket.receive': ws_message, 
    'websocket.disconnect': ws_disconnect, 
} 

在裝載該頁面,ws_add啓動但連接最終斷開。任何領導如何調試這個問題,或者是什麼問題。

我使用命令python manage.py runserver運行服務器。

編輯: 降級到twisted版本16.2.0。徒勞無功。

回答