aboutsummaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorJasper Van der Jeugt2020-08-05 15:48:27 +0200
committerJasper Van der Jeugt2020-08-05 15:48:27 +0200
commitb90901b2c2597a72ff6fe2de92d72db51455e577 (patch)
tree5ce24eee2535886c020ef7a11fb82bbd8decd2e7 /client
parent7ff45befe94cd248ea5505e4ca74005358d5e329 (diff)
Persistence with cookies
Diffstat (limited to 'client')
-rw-r--r--client/index.html46
1 files changed, 31 insertions, 15 deletions
diff --git a/client/index.html b/client/index.html
index 518b71d..007dc5c 100644
--- a/client/index.html
+++ b/client/index.html
@@ -11,24 +11,40 @@
<script type="text/JavaScript" src="$CAFP_BASE/assets/client.js"></script>
<script>
var app = Elm.Client.init({node: document.querySelector("main")});
- var protocol = "ws:";
- if(document.location.protocol == "https:") {
+
+ function connect() {
+ var protocol = "ws:";
+ if(document.location.protocol == "https:") {
protocol = "wss:"
- }
- var path = document.location.pathname;
- if(path.startsWith("$CAFP_BASE")) {
+ }
+ var path = document.location.pathname;
+ if(path.startsWith("$CAFP_BASE")) {
path = path.substr("$CAFP_BASE".length);
+ }
+ var roomId = path.split("/")[2];
+ var url = protocol + "//" + document.location.host +
+ "$CAFP_BASE/rooms/" + roomId + "/events";
+
+ var socket = new WebSocket(url);
+ var socketSend = function(message) {
+ socket.send(message);
+ };
+ app.ports.webSocketOut.subscribe(socketSend);
+ socket.onmessage = function(event) {
+ app.ports.webSocketIn.send(event.data);
+ };
+ socket.onclose = function(event) {
+ app.ports.webSocketOut.unsubscribe(socketSend);
+ setTimeout(function() {
+ connect();
+ }, 1000);
+ };
+ socket.onerror = function(event) {
+ socket.close();
+ };
}
- var roomId = path.split("/")[2];
- var url = protocol + "//" + document.location.host +
- "$CAFP_BASE/rooms/" + roomId + "/events";
- var socket = new WebSocket(url);
- app.ports.webSocketOut.subscribe(function(message) {
- socket.send(message);
- });
- socket.addEventListener("message", function(event) {
- app.ports.webSocketIn.send(event.data);
- });
+
+ connect();
</script>
</body>
</html>