Websocket client/server work - get statistic counter streaming working in place of the polling method

This commit is contained in:
Akkadius 2017-10-03 16:58:08 -05:00
parent 438effda5b
commit 279e3ed5bd
5 changed files with 61 additions and 35 deletions

37
app.js
View File

@ -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;

View File

@ -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");

View File

@ -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;

View File

@ -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);
};
}

View File

@ -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;