diff static/websocket.js @ 1041:a4632a7b2e17

upgrade knockout and jquery, simplify the static/ dirs for all web services Ignore-this: 8637b7b61cc5d38e8cf15bb1afd7466c
author Drew Perttula <drewp@bigasterisk.com>
date Wed, 28 May 2014 05:54:23 +0000
parents light9/rdfdb/web/websocket.js@6f984ce851e2
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/static/websocket.js	Wed May 28 05:54:23 2014 +0000
@@ -0,0 +1,20 @@
+function reconnectingWebSocket(url, onMessage) {
+    var pong = 0;
+    function connect() {
+        var ws = new WebSocket(url);
+        
+        ws.onopen = function() {   $("#status").text("connected"); };
+        ws.onerror = function(e) { $("#status").text("error: "+e); };
+        ws.onclose = function() {  
+            pong = 1 - pong;
+            $("#status").text("disconnected (retrying "+(pong ? "😼":"😺")+")"); 
+            // this should be under a requestAnimationFrame to
+            // save resources
+            setTimeout(connect, 2000);
+        };
+        ws.onmessage = function (evt) {
+            onMessage(JSON.parse(evt.data));
+        };
+    }
+    connect();
+}
\ No newline at end of file