diff options
author | Devaev Maxim <[email protected]> | 2018-12-15 04:29:40 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2018-12-15 04:29:40 +0300 |
commit | 3c33bd37190772a783369894e209bcfe0858177a (patch) | |
tree | e095f08f37371a3182f6ced0b280c4bcaa06983b /web/js/kvm/session.js | |
parent | 3445766a50eab16a96d969397a6fe0422f7cfcd2 (diff) |
own auth
Diffstat (limited to 'web/js/kvm/session.js')
-rw-r--r-- | web/js/kvm/session.js | 143 |
1 files changed, 0 insertions, 143 deletions
diff --git a/web/js/kvm/session.js b/web/js/kvm/session.js deleted file mode 100644 index e3e74160..00000000 --- a/web/js/kvm/session.js +++ /dev/null @@ -1,143 +0,0 @@ -function Session() { - // var self = this; - - /********************************************************************************/ - - var __ws = null; - - var __ping_timer = null; - var __missed_heartbeats = 0; - - var __hid = new Hid(); - var __atx = new Atx(); - var __msd = new Msd(); - var __streamer = new Streamer(); - - var __init__ = function() { - $("link-led").title = "Not connected yet..."; - __startPoller(); - }; - - /********************************************************************************/ - - var __setKvmdInfo = function(state) { - if (state.meta) { - var text = JSON.stringify(state.meta, undefined, 4).replace(/ /g, " ").replace(/\n/g, "<br>"); - $("about-meta").innerHTML = ` - <span class="code-comment">// The Pi-KVM metadata.<br> - // You can get this json using handle <a target="_blank" href="/kvmd/info">/kvmd/info</a>.<br> - // In the standard configuration this data<br> - // is specified in the file /etc/kvmd/meta.yaml.</span><br> - <br> - ${text} - `; - if (state.meta.server && state.meta.server.host) { - $("kvmd-meta-server-host").innerHTML = "Server: " + state.meta.server.host; - document.title = "Pi-KVM Session: " + state.meta.server.host; - } else { - $("kvmd-meta-server-host").innerHTML = ""; - document.title = "Pi-KVM Session"; - } - } - - $("about-version-kvmd").innerHTML = state.version.kvmd; - $("about-version-streamer").innerHTML = `${state.version.streamer} (${state.streamer})`; - }; - - var __startPoller = function() { - $("link-led").className = "led-yellow"; - $("link-led").title = "Connecting..."; - - var http = tools.makeRequest("GET", "/ws_auth", function() { - if (http.readyState === 4) { - if (http.status === 200) { - var proto = (location.protocol === "https:" ? "wss" : "ws"); - __ws = new WebSocket(`${proto}://${location.host}/kvmd/ws`); - __ws.onopen = __wsOpenHandler; - __ws.onmessage = __wsMessageHandler; - __ws.onerror = __wsErrorHandler; - __ws.onclose = __wsCloseHandler; - } else { - __wsCloseHandler(null); - } - } - }); - }; - - var __wsOpenHandler = function(event) { - tools.debug("Session: socket opened:", event); - $("link-led").className = "led-green"; - $("link-led").title = "Connected"; - __hid.setSocket(__ws); - __missed_heartbeats = 0; - __ping_timer = setInterval(__pingServer, 1000); - }; - - var __wsMessageHandler = function(event) { - // tools.debug("Session: received socket 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 === "info_state") { - __setKvmdInfo(event.msg.event_attrs); - } else if (event.msg.event === "atx_state") { - __atx.setState(event.msg.event_attrs); - } else if (event.msg.event === "msd_state") { - __msd.setState(event.msg.event_attrs); - } else if (event.msg.event === "streamer_state") { - __streamer.setState(event.msg.event_attrs); - } - } - }; - - var __wsErrorHandler = function(event) { - tools.error("Session: socket error:", event); - if (__ws) { - __ws.onclose = null; - __ws.close(); - __wsCloseHandler(null); - } - }; - - var __wsCloseHandler = function(event) { - tools.debug("Session: socket closed:", event); - - $("link-led").className = "led-gray"; - - if (__ping_timer) { - clearInterval(__ping_timer); - __ping_timer = null; - } - - __hid.setSocket(null); - __atx.setState(null); - __msd.setState(null); - __streamer.setState(null); - __ws = null; - - setTimeout(function() { - $("link-led").className = "led-yellow"; - setTimeout(__startPoller, 500); - }, 500); - }; - - var __pingServer = function() { - 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("Session: ping error:", err.message); - if (__ws) { - __ws.onclose = null; - __ws.close(); - __wsCloseHandler(null); - } - } - }; - - __init__(); -} |