diff options
-rw-r--r-- | www/js/utils.js | 49 |
1 files changed, 37 insertions, 12 deletions
diff --git a/www/js/utils.js b/www/js/utils.js index 29d1a84..3123c9c 100644 --- a/www/js/utils.js +++ b/www/js/utils.js @@ -37,18 +37,18 @@ Number.prototype.pad = function(size) { } var weekday = new Array(7); -weekday[0] = "Sonntag"; -weekday[1] = "Montag"; -weekday[2] = "Dienstag"; -weekday[3] = "Mittwoch"; -weekday[4] = "Donnerstag"; -weekday[5] = "Freitag"; -weekday[6] = "Samstag"; +weekday[0] = 'Sonntag'; +weekday[1] = 'Montag'; +weekday[2] = 'Dienstag'; +weekday[3] = 'Mittwoch'; +weekday[4] = 'Donnerstag'; +weekday[5] = 'Freitag'; +weekday[6] = 'Samstag'; var weekday_short = new Array('So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'); function format_datetime(d) { - if(Object.prototype.toString.call(d) === "[object Date]") { + if(Object.prototype.toString.call(d) === '[object Date]') { var datetimestr = weekday_short[d.getDay()]; datetimestr += ' ' + Number(d.getDate()).pad(2); datetimestr += '.' + Number(d.getMonth() + 1).pad(2); @@ -102,6 +102,7 @@ function Clock(draw_callback) { this.last_message = { t1: 0, t2: 0, t3: 0, t4: 0, tz_offset: 3600 }; this.clock_offset = 0; this.clock_rtt = 0; + this.state = 'NEW'; this.redraw = function() { var rdtime_ms = (+new Date()) + (this.last_message.tz_offset * 1000) + this.clock_offset; @@ -125,7 +126,7 @@ function Clock(draw_callback) { this.last_message = msg; this.clock_offset = ((msg.t2 - msg.t1) + (msg.t3 - msg.t4)) / 2; this.clock_rtt = (msg.t4 - msg.t1) - (msg.t3 - msg.t2); - console.log("got new ntp message from rhrdtime (rtt=" + this.clock_rtt + " ms): new offset = " + this.clock_offset + " ms"); + console.log('got new ntp message from rhrdtime (rtt=' + this.clock_rtt + ' ms): new offset = ' + this.clock_offset + ' ms'); } this.ntp_request = function() { @@ -133,17 +134,41 @@ function Clock(draw_callback) { } this.sock_onopen = function() { + console.log('clock websocket connection established'); + this.state = 'CONNECTED'; this.ntp_request(); - this.interval_request = setInterval(this.ntp_request.bind(this), 5000); + this.interval_request = setInterval(this.ntp_request.bind(this), 2000); } - this.start = function() { - this.sock = new WebSocket("wss://" + window.location.host + "/ntp"); + this.sock_onclose = function(event) { + if(this.state == 'STOPPED') { + delete this.sock; + } else { + console.log('clock websocket closed with code ' + event.code + ', trying reconnect...'); + clearInterval(this.interval_request); + delete this.interval_request; + this.sock.close(); + delete this.sock; + setTimeout(this.connect.bind(this), 1000); + this.state = 'RECONNECTING'; + } + } + + this.connect = function() { + this.sock = new WebSocket('wss://' + window.location.host + '/ntp'); this.sock.onmessage = this.ntp_update.bind(this); this.sock.onopen = this.sock_onopen.bind(this); + this.sock.onclose = this.sock_onclose.bind(this); + this.state = 'CONNECTING'; + } + + this.start = function() { + this.connect(); this.interval_redraw = setInterval(this.redraw.bind(this), 200); } + this.stop = function() { + this.state = 'STOPPED'; clearInterval(this.interval_redraw); delete this.interval_redraw; clearInterval(this.interval_request); |