aboutsummaryrefslogtreecommitdiff
path: root/juick-ws/src/main/static/scripts.js
blob: 3a7e4a5f15c584577e6e51b142f6de7beabd7627 (plain) (blame)
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
var Terminal = require('terminal');
require('whatwg-fetch');

function ready(fn) {
    if (document.readyState != 'loading') {
        fn();
    } else {
        document.addEventListener('DOMContentLoaded', fn);
    }
}

function refreshStatus() {
    setTimeout(function() {
        fetch("/api/status")
                    .then(function(response) {
                        return response.text();
                    }).then(function(body) {
                        status.textContent = JSON.parse(body).status;
                        refreshStatus();
                    })
    }, 5000);
}

function keepalive(ws) {
    setTimeout(function() {
            ws.send(" ");
            keepalive();
        }, 60000);
}

ready(function() {
    var ws = new WebSocket('wss://ws.juick.com/');
    var term = new Terminal('terminal', {}, {});
    var status = document.querySelector("#status");
    ws.onopen = function() {
        term.output("<br/>connected");
    };
    ws.onclose = function() {
        term.output("<br/>disconnected");
    }
    ws.onmessage = function(msg) {
        term.output("<br/>" + JSON.stringify(JSON.parse(msg.data), null, 2));
    }
    refreshStatus();
    keepalive(ws);
})