2017-01-24 6 views
2

私はDjangoチャンネルを使用してブラウザとの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がWebSocketのために使用:ロードオン

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にダウングレードしました。役立たず。

答えて

4

またwebsocket.connectのための消費者を持っていると、プロトコルサーバは、WebSocketをハンドシェイクを完了します接続のみを受け入れた後、接続を受け入れる必要があり

channel_routing = { 
    "websocket.connect": consumers.ws_connect, 
} 

consumers.py

def ws_connect(message): 
    message.reply_channel.send({ 
     'accept': True 
}) 

http://channels.readthedocs.io/en/latest/releases/1.0.0.html#websocket-accept-reject-flow

関連する問題