diff --git a/static/scripts/graph.js b/static/scripts/graph.js index 56d48b6..07252a2 100644 --- a/static/scripts/graph.js +++ b/static/scripts/graph.js @@ -3,7 +3,7 @@ graph.colors = ["red", "green"]; graph.prepare = function() { materials.prepare(); - websocket.connect(graph.update); + //poller.connect(graph.update); }; graph.center = function() { diff --git a/static/scripts/poller.js b/static/scripts/poller.js new file mode 100644 index 0000000..63cc419 --- /dev/null +++ b/static/scripts/poller.js @@ -0,0 +1,23 @@ +function poll() { + var xmlhttp = new XMLHttpRequest(); + xmlhttp.onreadystatechange = function() { + if (xmlhttp.readyState == XMLHttpRequest.DONE) { + if (xmlhttp.status == 200) { + update(xmlhttp.responseText); + } + } + }; + + // Send the request + xmlhttp.open("GET", "/graph", true); + xmlhttp.send(); +}; + + +function update(s) { + json = JSON.parse(s); + graph.update(json); +} + +// Launch the polling loop +setInterval(poll, 1000); diff --git a/static/scripts/websocket.js b/static/scripts/websocket.js deleted file mode 100644 index 919b9d2..0000000 --- a/static/scripts/websocket.js +++ /dev/null @@ -1,36 +0,0 @@ -var websocket = {}; -websocket.update = undefined; - -websocket.connect = function(update) { - websocket.ws = new WebSocket("ws://localhost:5000"); - if (update){ - websocket.update = update; - } - websocket.ws.onopen = function(evt) { - gui.serverMessage("Connected to server."); - }; - - websocket.ws.onerror = function(err) { - gui.serverMessage("Could not connect to server."); - }; - - websocket.ws.onmessage = function(evt) { - json = JSON.parse(evt.data); - for (var i in json.node) { - var pos = json.node[i].position; - json.node[i].position = new THREE.Vector3(pos.x, pos.y, pos.z); - if (json.node[i].vop === undefined){ - json.node[i].vop = 0; - } - } - websocket.update(json); - }; - - websocket.ws.onclose = function(evt) { - gui.serverMessage("No connection to server. Reconnect.", true); - }; -}; - -websocket.edit = function (data) { - websocket.ws.send("edit:"+JSON.stringify(data)); -}; diff --git a/templates/index.html b/templates/index.html index 040e9a7..9cac39c 100644 --- a/templates/index.html +++ b/templates/index.html @@ -14,13 +14,13 @@ - + - - + + diff --git a/test.py b/test.py index 9e159a5..918aaf0 100644 --- a/test.py +++ b/test.py @@ -6,14 +6,15 @@ import json import abp def test_graph(): - N = 1000 - g = abp.GraphState(range(N), vop="hadamard") + N = 100 + g = abp.NXGraphState(range(N), vop="hadamard") for i in range(N-1): g.act_cz(i, i+1) + g.layout() return g if __name__ == '__main__': - data = json.dumps(test_graph().to_json()) + data = json.dumps(test_graph().to_json(stringify=False)) r = requests.post("http://localhost:5000/graph", data=data) print r.status_code, r.content