aboutsummaryrefslogtreecommitdiff
path: root/client/index.html
blob: a03d777472a8ac40ae5db2ff30718352933ece11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Client</title>
    <link rel="stylesheet" type="text/css" href="$UPLCG_BASE/assets/style.css">
    <meta name="viewport" content="width=device-width">
  </head>
  <body>
    <div id="main"></div>
    <script type="text/JavaScript" src="$UPLCG_BASE/assets/client.js"></script>
    <script>
      var app = Elm.Client.init({node: document.querySelector("main")});

      function connect() {
        var protocol = "ws:";
        if(document.location.protocol == "https:") {
          protocol = "wss:"
        }
        var path = document.location.pathname;
        if(path.startsWith("$UPLCG_BASE")) {
          path = path.substr("$UPLCG_BASE".length);
        }
        var roomId = path.split("/")[2];
        var url = protocol + "//" + document.location.host +
          "$UPLCG_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();
        };
      }

      connect();
    </script>
    <footer>Untitled PL Card Game version $UPLCG_VERSION</footer>
  </body>
</html>