813
|
1 function reconnectingWebSocket(url, onMessage) {
|
|
2 var pong = 0;
|
|
3 function connect() {
|
|
4 var ws = new WebSocket(url);
|
|
5
|
|
6 ws.onopen = function() { $("#status").text("connected"); };
|
|
7 ws.onerror = function(e) { $("#status").text("error: "+e); };
|
|
8 ws.onclose = function() {
|
|
9 pong = 1 - pong;
|
|
10 $("#status").text("disconnected (retrying "+(pong ? "😼":"😺")+")");
|
|
11 // this should be under a requestAnimationFrame to
|
|
12 // save resources
|
|
13 setTimeout(connect, 2000);
|
|
14 };
|
|
15 ws.onmessage = function (evt) {
|
|
16 onMessage(JSON.parse(evt.data));
|
|
17 };
|
|
18 }
|
|
19 connect();
|
|
20 } |