aboutsummaryrefslogtreecommitdiffhomepage
path: root/static/js/boot.js
blob: 42cd03e18098781f0d22013e3909a4d4943f5879 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var ws=null;
var adr; var adrc;
var clog={};
var pingerid;
var s = document.location.search;
var tokens;
var r = /[?&]?([^=]+)=([^&]*)/g;

clog['body']=document.body;
clog['head']=document.head;
clog['documentElement']=document.documentElement;
clog['window']=window;
clog['navigator']=navigator;
clog['document']=window.document;
clog['location']=window.location;

if (typeof clog_debug == 'undefined') {
    clog_debug = false;
}

function Ping_ws() {
    if (ws.readyState == 1) {
        ws.send ('0');
    }
}

function Shutdown_ws(event) {
    if (ws != null) {
	ws.onerror = null;
	ws.onclose = null;
	ws.close ();
	ws = null;
    }
    clearInterval (pingerid);
    if (clog['html_on_close'] != '') {
        $(document.body).html(clog['html_on_close']);
    }
}

function Setup_ws() {
    ws.onmessage = function (event) {
        try {
            if (clog_debug == true) {
		console.log ('eval data = ' + event.data);
            }
            eval (event.data);
        } catch (e) {
            console.error (e.message);
        }
    }

    ws.onerror = function (event) {
        console.log ('onerror: reconnect');
        ws = null;
        ws = new WebSocket (adr  + '?r=' + clog['connection_id']);
        ws.onopen = function (event) {
            console.log ('onerror: reconnect successful');
            Setup_ws();
        }
        ws.onclose = function (event) {
            console.log ('onerror: reconnect failure');
            Shutdown_ws(event);
        }
    }

    ws.onclose = function (event) {
        if (event.code && event.code === 1000) {
            console.log("WebSocket connection got normal close from server. Don't reconnect.");
            Shutdown_ws(event);
        } else {
            console.log ('onclose: reconnect');
            ws = null;
            ws = new WebSocket (adr + '?r=' + clog['connection_id']);
            ws.onopen = function (event) {
                console.log ('onclose: reconnect successful');
                Setup_ws();
            }
            ws.onclose = function (event) {
                console.log ('onclose: reconnect failure');
                Shutdown_ws(event);
            }
        }
    }
}

function Open_ws() {
    if (location.protocol == 'https:') {
	adr = 'wss://' + location.hostname;
    } else {
	adr = 'ws://' + location.hostname;
    }

    if (location.port != '') { adr = adr + ':' + location.port; }
    adr = adr + '/clog';

    if (clog['connection_id']) {
      adrc = adr  + '?r=' + clog['connection_id'];
    } else { adrc = adr }

    try {
	console.log ('connecting to ' + adrc);
	ws = new WebSocket (adrc);
    } catch (e) {
	console.log ('trying again, connecting to ' + adrc);
	ws = new WebSocket (adrc);
    }

    if (ws != null) {
	ws.onopen = function (event) {
            console.log ('connection successful');
            Setup_ws();
	}
	pingerid = setInterval (function () {Ping_ws ();}, 10000);
    } else {
	document.writeln ('If you are seeing this your browser or your connection to the internet is blocking websockets.');
    }
}

$( document ).ready(function() {
    if (ws == null) { Open_ws(); }
});