Mercurial > code > home > repos > light9
annotate light9/web/websocket.js @ 1873:f001d689b3e2
more py3 and typing fixes
Ignore-this: 3180bd966cac69de56b86ef6a308cad4
author | Drew Perttula <drewp@bigasterisk.com> |
---|---|
date | Mon, 27 May 2019 06:20:38 +0000 |
parents | e703b3434dbd |
children | 19c2e6216cf8 |
rev | line source |
---|---|
1217
e703b3434dbd
websocket and web cleanup
Drew Perttula <drewp@bigasterisk.com>
parents:
1212
diff
changeset
|
1 /* |
1873
f001d689b3e2
more py3 and typing fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
1217
diff
changeset
|
2 url is now relative to the window location. Note that nginx may drop |
f001d689b3e2
more py3 and typing fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
1217
diff
changeset
|
3 the connection after 60sec of inactivity. |
1217
e703b3434dbd
websocket and web cleanup
Drew Perttula <drewp@bigasterisk.com>
parents:
1212
diff
changeset
|
4 */ |
813 | 5 function reconnectingWebSocket(url, onMessage) { |
6 var pong = 0; | |
1217
e703b3434dbd
websocket and web cleanup
Drew Perttula <drewp@bigasterisk.com>
parents:
1212
diff
changeset
|
7 |
e703b3434dbd
websocket and web cleanup
Drew Perttula <drewp@bigasterisk.com>
parents:
1212
diff
changeset
|
8 var fullUrl = ( |
e703b3434dbd
websocket and web cleanup
Drew Perttula <drewp@bigasterisk.com>
parents:
1212
diff
changeset
|
9 "ws://" |
e703b3434dbd
websocket and web cleanup
Drew Perttula <drewp@bigasterisk.com>
parents:
1212
diff
changeset
|
10 + window.location.host |
e703b3434dbd
websocket and web cleanup
Drew Perttula <drewp@bigasterisk.com>
parents:
1212
diff
changeset
|
11 + window.location.pathname |
e703b3434dbd
websocket and web cleanup
Drew Perttula <drewp@bigasterisk.com>
parents:
1212
diff
changeset
|
12 + (window.location.pathname.match(/\/$/) ? "" : "/") |
e703b3434dbd
websocket and web cleanup
Drew Perttula <drewp@bigasterisk.com>
parents:
1212
diff
changeset
|
13 + url); |
813 | 14 function connect() { |
1217
e703b3434dbd
websocket and web cleanup
Drew Perttula <drewp@bigasterisk.com>
parents:
1212
diff
changeset
|
15 var ws = new WebSocket(fullUrl); |
813 | 16 |
17 ws.onopen = function() { $("#status").text("connected"); }; | |
18 ws.onerror = function(e) { $("#status").text("error: "+e); }; | |
19 ws.onclose = function() { | |
20 pong = 1 - pong; | |
21 $("#status").text("disconnected (retrying "+(pong ? "😼":"😺")+")"); | |
22 // this should be under a requestAnimationFrame to | |
23 // save resources | |
24 setTimeout(connect, 2000); | |
25 }; | |
26 ws.onmessage = function (evt) { | |
27 onMessage(JSON.parse(evt.data)); | |
28 }; | |
29 } | |
30 connect(); | |
1217
e703b3434dbd
websocket and web cleanup
Drew Perttula <drewp@bigasterisk.com>
parents:
1212
diff
changeset
|
31 } |