summaryrefslogtreecommitdiffstats
path: root/virtual-programs/web/web-keyboards.folk
blob: b1668c69b8687a8bd9255e3e4c67af851ff84e25 (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
52
53
54
When the keyboards are /keyboards/ {
  Wish the web server handles route "/keyboards$" with handler [list apply {{keyboards} {
    upvar ^html ^html
    html [subst {
      <html>
      <body>
        <span id="status">Status</span>
        <dl>
        [join [lmap kb $keyboards { subst {
            <dt>$kb</dt>
            <dd>
              <code id="$kb-presses"></code><br>
              <button onclick="send(`Assert web-keyboards wishes to print {Claim \$this is a keyboard with path $kb} with job-id ` + String(Math.random()))">Print</button>
            </dd>
        } }] "\n"]
        </dl>

        <script>
        function wsConnect() {
          ws = new WebSocket(window.location.origin.replace("http", "ws") + "/ws");
          send = function(s) { ws.send(s); }

          ws.onopen = () => {
            document.getElementById('status').innerHTML = "<span style=background-color:seagreen;color:white;>Connnected</span>";
            send(`
              Assert when keyboard /kb/ claims key /k/ is down timestamp /ts/ {{chan kb k ts} {
                if {\$ts > [clock milliseconds]} {
                  ::websocket::send \$chan text "\$kb||\$k"
                }
              }} with environment \[list \$chan]
            `);
          };
          ws.onclose = window.onbeforeunload = () => {
            document.getElementById('status').innerHTML = "<span style=background-color:red;color:white;>Disconnnected</span>";
            send(`Retract when keyboard /kb/ claims key /k/ is down timestamp /ts/ /anything/ with environment \[list \$chan]`)
            setTimeout(() => { wsConnect(); }, 1000);
          };
          ws.onerror = (err) => {
            document.getElementById('status').innerText = "Error";
            console.error('Socket encountered error: ', err.message, 'Closing socket');
            ws.close();
          }
          ws.onmessage = (msg) => {
            const \[kb, key] = msg.data.split("||");
            document.getElementById(kb + "-presses").innerText += key + " ";
          }
        };
        wsConnect();
      </script>
      </body>
      </html>
    }]
  }} $keyboards]
}