diff options
author | Devaev Maxim <[email protected]> | 2018-07-15 11:21:44 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2018-07-15 11:21:44 +0300 |
commit | 999d3f245710013425cc2413f81d900ec1fc2f24 (patch) | |
tree | 07bf519cab5da1b522e8146a362724762c8a3e7c /kvmd/web/js/session.js | |
parent | 4122ecdb55abae00f2168d27df5f88527fc02341 (diff) |
big js refactoring
Diffstat (limited to 'kvmd/web/js/session.js')
-rw-r--r-- | kvmd/web/js/session.js | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/kvmd/web/js/session.js b/kvmd/web/js/session.js new file mode 100644 index 00000000..31a76f68 --- /dev/null +++ b/kvmd/web/js/session.js @@ -0,0 +1,61 @@ +var session = new function() { + var __ws = null; + var __ping_timer = null; + var __missed_heartbeats = 0; + + this.startPoller = function() { + __ws = new WebSocket("ws://" + location.host + "/kvmd/ws"); + __ws.onopen = __wsOpenHandler; + __ws.onmessage = __wsMessageHandler; + __ws.onerror = __wsErrorHandler; + __ws.onclose = __wsCloseHandler; + }; + + var __wsOpenHandler = function(event) { + tools.debug("WebSocket opened:", event); + hid.installCapture(__ws); + __missed_heartbeats = 0; + __ping_timer = setInterval(__pingServer, 1000); + }; + + var __wsMessageHandler = function(event) { + // tools.debug("WebSocket: received data:", event.data); + event = JSON.parse(event.data); + if (event.msg_type === "pong") { + __missed_heartbeats = 0; + } else if (event.msg_type === "event") { + if (event.msg.event === "atx_state") { + atx.setLedsState(event.msg.event_attrs.leds); + } + } + }; + + var __wsErrorHandler = function(event) { + tools.error("WebSocket error:", event); + __ws.close(); + }; + + var __wsCloseHandler = function(event) { + tools.debug("WebSocket closed:", event); + if (__ping_timer) { + clearInterval(__ping_timer); + __ping_timer = null; + } + hid.clearCapture(); + atx.clearLeds(); + setTimeout(session.startPoller, 1000); + }; + + var __pingServer = function(event) { + try { + __missed_heartbeats += 1; + if (__missed_heartbeats >= 5) { + throw new Error("Too many missed heartbeats"); + } + __ws.send(JSON.stringify({"event_type": "ping"})); + } catch (err) { + tools.error("Ping error:", err.message); + __ws.close(); + } + }; +}; |