From 9bab6789431df1d08a4c26978a1312f160382fd3 Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Wed, 18 May 2016 01:27:47 +0100 Subject: [PATCH] Somehow lost the websocket interface --- static/index.html | 1 + static/main.js | 2 +- static/websocket_interface.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 static/websocket_interface.js diff --git a/static/index.html b/static/index.html index d114288..d3c047f 100644 --- a/static/index.html +++ b/static/index.html @@ -15,6 +15,7 @@ + diff --git a/static/main.js b/static/main.js index f2589bb..c92b53c 100644 --- a/static/main.js +++ b/static/main.js @@ -52,8 +52,8 @@ function init() { controls = new THREE.OrbitControls(camera); controls.center.set(0, 0, 0); controls.rotateSpeed = 0.2; - controls.addEventListener("change", render); camera.position.set(0, 0, 20); + controls.addEventListener("change", render); // Run scene = makeScene(); diff --git a/static/websocket_interface.js b/static/websocket_interface.js new file mode 100644 index 0000000..843a09c --- /dev/null +++ b/static/websocket_interface.js @@ -0,0 +1,34 @@ +var ws; + +function connect_to_server() { + ws = new WebSocket("ws://localhost:5000"); + ws.onopen = function() + { + message.innerHTML = "Connected to server."; + message.className = "visible"; + }; + + ws.onerror = function(err) + { + message.innerHTML = "Could not connect to server."; + message.className = "visible"; + }; + + ws.onmessage = function (evt) + { + console.log("Received update"); + var new_state = JSON.parse(evt.data); + vops = new_state.vops; + ngbh = new_state.ngbh; + meta = new_state.meta; + updateScene(); + }; + + ws.onclose = function() + { + message.innerHTML = "Connection to server lost. Reconnect."; + message.className = "visible"; + }; +} + +