From 279e3ed5bd3af6125a0d7a31438830af3aa850a0 Mon Sep 17 00:00:00 2001 From: Akkadius Date: Tue, 3 Oct 2017 16:58:08 -0500 Subject: [PATCH] Websocket client/server work - get statistic counter streaming working in place of the polling method --- app.js | 37 ++++++++++++++--------- public/assets/js/glass-core.js | 27 ++++++++++++----- public/assets/js/glass-pages/dhcp-logs.js | 6 ++-- public/assets/js/glass-websockets.js | 13 ++++++-- public/templates/index.html | 13 +++----- 5 files changed, 61 insertions(+), 35 deletions(-) diff --git a/app.js b/app.js index bd9f49f..1f328a5 100644 --- a/app.js +++ b/app.js @@ -229,20 +229,14 @@ leases_per_minute_counter_timer = setInterval(function(){ /* Websockets statistics subscription broadcast */ - if(ws_is_subscribed('dhcp_statistics')) { - + if(ws_event_subscribers('dhcp_statistics')) { return_data = { "cpu_utilization": cpu_utilization, "leases_per_second": current_leases_per_second, "leases_per_minute": leases_per_minute }; wss.broadcast_event(JSON.stringify(return_data), 'dhcp_statistics'); - - console.log('Returning stats'); } - else { - console.log('Stats not subscribed'); - } }, 1000); @@ -392,14 +386,29 @@ function isJson(str) { return true; } -function ws_is_subscribed(event){ - if(typeof ws === "undefined") - return 0; +function ws_event_subscribers(event){ + if(typeof wss === "undefined") + return false; - if(ws.event_subscription[event]) - return 1; + var is_listening = false; - return 0; + wss.clients.forEach(function each(ws) { + + /* Count event listeners */ + for (var event_listening in ws.event_subscription) { + if (event_listening == event) { + is_listening = true; + return true; + } + } + + }); + + if (is_listening) { + return true; + } + + return false; } wss.on('connection', function connection(ws) { @@ -462,7 +471,7 @@ function stale_connections_audit() { /* Keepalive - kill stale connections (30s poll) */ const interval = setInterval(function ping() { stale_connections_audit(); -}, 30000); +}, 3000); var socket_clients = 0; diff --git a/public/assets/js/glass-core.js b/public/assets/js/glass-core.js index c49c75a..f08e525 100644 --- a/public/assets/js/glass-core.js +++ b/public/assets/js/glass-core.js @@ -36,6 +36,12 @@ $(document).ready(function () { } } }); + + + }, 100); + + setTimeout(function(){ + handle_websocket_subscription_navigation (); }, 100); }); @@ -50,11 +56,6 @@ $(document).on('on_pjax_click', function (e, href) { /* Unhook from all websocket events */ websockets_unsubscribe_all_events(); - /* Stream dashboard stats */ - if(document.location.pathname == "/"){ - websockets_subscribe_event("dhcp_statistics"); - } - }); /* @@ -66,6 +67,8 @@ $(document).on('on_pjax_complete', function (e) { $('.overlay').css("display", "none"); } + handle_websocket_subscription_navigation (); + /* * Form input focus event */ @@ -99,10 +102,20 @@ $(document).on('on_pjax_complete', function (e) { }); remove_init_form(); - - remove_init_form(); }); +function handle_websocket_subscription_navigation () { + console.log(window.location.pathname); + + /* Stream dashboard stats */ + if(document.location.pathname == "/"){ + websockets_subscribe_event("dhcp_statistics"); + } + else { + websockets_unsubscribe_all_events(); + } +} + function remove_init_form() { setTimeout(function () { $('.form-line').removeClass("focused"); diff --git a/public/assets/js/glass-pages/dhcp-logs.js b/public/assets/js/glass-pages/dhcp-logs.js index acdb5c0..0b0c3e1 100644 --- a/public/assets/js/glass-pages/dhcp-logs.js +++ b/public/assets/js/glass-pages/dhcp-logs.js @@ -77,9 +77,9 @@ function parse_log_stream (console_data){ } /* - Note: the only thing I stream currently is dhcp log - so later incoming messages will need to be - keyed by their "type" via json - */ + Note: the only thing I stream currently is dhcp log - so later incoming messages will need to be + keyed by their "type" via json + */ var grep_value = document.getElementById("grep_fitler").value; diff --git a/public/assets/js/glass-websockets.js b/public/assets/js/glass-websockets.js index 3905b79..f051db4 100644 --- a/public/assets/js/glass-websockets.js +++ b/public/assets/js/glass-websockets.js @@ -18,8 +18,12 @@ function websockets_unsubscribe_event(event){ } function websockets_unsubscribe_all_events(){ - socket.send(JSON.stringify({"event_unsubscribe": "all_events"})); - subscribed_events = []; + if(typeof subscribed_events !== "undefined") { + if(subscribed_events.length > 0 ) { + socket.send(JSON.stringify({"event_unsubscribe": "all_events"})); + subscribed_events = []; + } + } } function connect_websocket() { @@ -36,7 +40,7 @@ function connect_websocket() { var event_data = JSON.parse(data.data); /* - {event: "dhcp_log_subscription", data: "Oct 3 16:02:59 DHCP-Server dhcpd[5303]: reuse_l…% threshold, reply with unaltered, existing lease"} + {event: "dhcp_log_subscription", data: "Oct 3 16:02:59 DHCP-Server dhcpd[5303]: reuse_l…% threshold, reply with unaltered, existing lease"} */ if (!subscribed_events[event_data['event']]) @@ -46,6 +50,9 @@ function connect_websocket() { if (event_data['event'] == 'dhcp_log_subscription') parse_log_stream (event_data.data); + if (event_data['event'] == 'dhcp_statistics') + parse_statistics_stream (event_data.data); + }; } diff --git a/public/templates/index.html b/public/templates/index.html index a61fbaa..ddb2f56 100644 --- a/public/templates/index.html +++ b/public/templates/index.html @@ -283,14 +283,11 @@ } }, 5000); - var get_counters_timer = setInterval(function () { - if ($('#total-leases').length) { - $.get( "/api/get_server_info", function( data ) { - $('#cpu-utilization').html(data.cpu_utilization + "%"); - $('#leases-sec').html(data.leases_per_second.toLocaleString('en') + ' / ' + data.leases_per_minute.toLocaleString('en')); - }); - } - }, 1000); + function parse_statistics_stream(data){ + data = JSON.parse(data); + $('#cpu-utilization').html(data.cpu_utilization + "%"); + $('#leases-sec').html(data.leases_per_second.toLocaleString('en') + ' / ' + data.leases_per_minute.toLocaleString('en')); + } var window_focus = true;