<script>
var ws;
+ var h = new Object();
- function process(key,value) {
+ function update_h(key, value) {
+ h[key] = value;
+ }
+
+ function fill_html(key,value) {
var d = document.getElementById(key);
if (d !== null) {
d.innerHTML = value;
}
}
- function traverse(o) {
+ function traverse(o, func) {
console.log(o);
for (var i in o) {
- process(i, o[i]);
if (o[i] !== null && typeof(o[i])=="object") {
- traverse(o[i]);
+ traverse(o[i], func);
+ } else {
+ func(i, o[i]);
}
}
}
-
- window.onload = function() {
+ function startws() {
ws = new WebSocket('<%= $url->to_abs %>');
- if (typeof(ws) === 'object') {
+ if (typeof(ws) !== null) {
ws.onmessage = function (event) {
var js = JSON.parse(event.data);
if (js !== null && typeof(js) === 'object') {
- traverse(js);
+ traverse(js, fill_html);
+ traverse(js, update_h);
+ document.getElementById("hh").innerHTML = JSON.stringify(h);
}
};
ws.onopen = function (event) {
};
ws.onclose = function(event) {
document.getElementById("wsconnect").innerHTML = 'ws disconnected, refresh to restart';
+ ws = null;
}
} else {
document.body.innerHTML += 'Webserver only works with Websocket aware browsers';
}
+ }
+
+ window.onload = function() {
+ startws();
+ window.setInterval(function() {
+ if (ws === null)
+ startws();
+ }, 15000);
}
</script>
</table>
<br>
<div id="wsconnect" align="center"> </div>
+ <br>
+ <div id="hh" align="center"> </div>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->