django channel set up routing -
i'm learning how setup websocket connection django-channel, i've got setup in routing.py
from channels import route gallery import socket sock channel_routing = [ # wire websocket channels our consumers: route("websocket.connect", sock.ws_connect, path=r"^/render-status/$"), route("websocket.receive", sock.ws_receive, , path=r"^/render-status/$"), ]
and following 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 " + 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 started, add new item table if (data.action == "started") { } // if action completed, update status else if (data.action == "completed"){ } }; var message = { action: "incomplete", job_name: "10", }; socket.send(json.stringify(message));
tried out , there's failure in connecting (from console)
colorpicker2.js:565 connecting ws://127.0.0.1:8000/render-status/ reconnecting-websocket.min.js:1 uncaught domexception: failed execute 'send' on 'websocket': still in connecting state. @ a.send (http://127.0.0.1:8000/static/js/reconnecting-websocket.min.js:1:2611) @ htmldocument.<anonymous> (http://127.0.0.1:8000/static/js/colorpicker2.js:584:11) @ k (http://127.0.0.1:8000/static/js/jquery.js:15:16962) @ object.firewith [as resolvewith] (http://127.0.0.1:8000/static/js/jquery.js:15:17720) @ function.ready (http://127.0.0.1:8000/static/js/jquery.js:15:12435) @ 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 'ws://127.0.0.1:8000/render-status/' failed: error during websocket handshake: unexpected response code: 404 open @ reconnecting-websocket.min.js:1 @ 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 '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
i've checked documentation https://channels.readthedocs.io/en/stable/routing.html sure how set path.
your routing correct, sending message soon, while socket still trying connect. use onopen
callback ensure message sent after connection established:
socket.onopen = function() { var message = { action: "incomplete", job_name: "10", }; socket.send(json.stringify(message)); }
wiki
Comments
Post a Comment