Mercurial > code > home > repos > light9
view static/websocket.js @ 1053:9937e2e3d17b
effecteval faster loop, stats page
Ignore-this: d107f2e01d73fc6629249e5fec98cb85
author | Drew Perttula <drewp@bigasterisk.com> |
---|---|
date | Sun, 01 Jun 2014 10:05:50 +0000 |
parents | a4632a7b2e17 |
children |
line wrap: on
line source
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(); }