2017-08-21 4 views
0

をルーティング設定、私はrouting.pyジャンゴチャネルは、私がどのようにセットアップするジャンゴ・チャネルとのWebSocket接続を学んでいる

from channels import route 
from gallery import socket as sock 

channel_routing = [ 
    # Wire up websocket channels to our consumers: 
    route("websocket.connect", sock.ws_connect, path=r"^/render-status/$"), 
    route("websocket.receive", sock.ws_receive, , path=r"^/render-status/$"), 
] 

と次のJavaScript

みました
// When we're using HTTPS, use WSS too. 
var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws"; 
var ws_path = ws_scheme + '://' + window.location.host + '/render-status/'; 
console.log("Connecting to " + ws_path) 
var socket = new ReconnectingWebSocket(ws_path); 
socket.onmessage = function(message) { 
    console.log("Got message: " + message.data); 
    var data = JSON.parse(message.data); 
    // if action is started, add new item to table 
    if (data.action == "started") { 

    } 
    // if action is completed, just update the status 
    else if (data.action == "completed"){ 

    } 
}; 

var message = { 
    action: "incomplete", 
    job_name: "10", 
}; 
socket.send(JSON.stringify(message)); 

でこの設定を持っています(コンソールから)接続に失敗しました

colorpicker2.js:565 Connecting to ws://127.0.0.1:8000/render-status/ 
reconnecting-websocket.min.js:1 Uncaught DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state. 
    at a.send (http://127.0.0.1:8000/static/js/reconnecting-websocket.min.js:1:2611) 
    at HTMLDocument.<anonymous> (http://127.0.0.1:8000/static/js/colorpicker2.js:584:11) 
    at k (http://127.0.0.1:8000/static/js/jquery.js:15:16962) 
    at Object.fireWith [as resolveWith] (http://127.0.0.1:8000/static/js/jquery.js:15:17720) 
    at Function.ready (http://127.0.0.1:8000/static/js/jquery.js:15:12435) 
    at HTMLDocument.D (http://127.0.0.1:8000/static/js/jquery.js:15:9840) 
send @ reconnecting-websocket.min.js:1 
(anonymous) @ colorpicker2.js:584 
k @ jquery.js:15 
fireWith @ jquery.js:15 
ready @ jquery.js:15 
D @ jquery.js:15 
reconnecting-websocket.min.js:1 WebSocket connection to 'ws://127.0.0.1:8000/render-status/' failed: Error during WebSocket handshake: Unexpected response code: 404 
open @ reconnecting-websocket.min.js:1 
a @ reconnecting-websocket.min.js:1 
(anonymous) @ colorpicker2.js:566 
k @ jquery.js:15 
fireWith @ jquery.js:15 
ready @ jquery.js:15 
D @ jquery.js:15 
22reconnecting-websocket.min.js:1 WebSocket connection to 'ws://127.0.0.1:8000/render-status/' failed: Error during WebSocket handshake: Unexpected response code: 404 
open @ reconnecting-websocket.min.js:1 
(anonymous) @ reconnecting-websocket.min.js:1 

また、これがパスを設定する方法であることを確認してください。

答えて

0

ルーティングは正しいですが、ソケットがまだ接続しようとしている間にメッセージを送信するのが早すぎます。メッセージは、接続が確立された後にのみ送信されることを確認するためにonopenコールバックを使用します。

socket.onopen = function() { 
    var message = { 
     action: "incomplete", 
     job_name: "10", 
    }; 
    socket.send(JSON.stringify(message)); 
} 
+0

あなたの権利は、最終的にこれとスニペットが含まれており、今で同様の問題が発生して –

+0

をそれをすべてのセットアップに取り組んでいはHTTPSを助けるかもしれない願っています: //stackoverflow.com/questions/46197761/finish-setting-up-django-channel-celery-to-have-background-process-async-to-si –

関連する問題