Reconfigure websockets core server/client code to differentiate data subscription types
This commit is contained in:
parent
6eea9949b1
commit
7ed986d0d7
33
app.js
33
app.js
|
@ -227,6 +227,23 @@ leases_per_minute_counter_timer = setInterval(function(){
|
|||
if (leases_per_minute_counter == 60)
|
||||
leases_per_minute_counter = 0;
|
||||
|
||||
|
||||
/* Websockets statistics subscription broadcast */
|
||||
if(ws_is_subscribed('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);
|
||||
|
||||
/**
|
||||
|
@ -375,6 +392,16 @@ function isJson(str) {
|
|||
return true;
|
||||
}
|
||||
|
||||
function ws_is_subscribed(event){
|
||||
if(typeof ws === "undefined")
|
||||
return 0;
|
||||
|
||||
if(ws.event_subscription[event])
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
wss.on('connection', function connection(ws) {
|
||||
ws.isAlive = true;
|
||||
ws.on('pong', heartbeat);
|
||||
|
@ -391,6 +418,10 @@ wss.on('connection', function connection(ws) {
|
|||
console.log("[WS] event_unsubscribe '%s'", json['event_unsubscribe']);
|
||||
delete ws.event_subscription[json["event_unsubscribe"]];
|
||||
}
|
||||
if(typeof json["all_events"] !== "undefined"){
|
||||
console.log("[WS] event_unsubscribe '%s'", json['event_unsubscribe']);
|
||||
ws.event_subscription = [];
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -409,7 +440,7 @@ wss.broadcast_event = function broadcast(data, event) {
|
|||
wss.clients.forEach(function each(client) {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
if(client.event_subscription[event])
|
||||
client.send(data);
|
||||
client.send(JSON.stringify({"event": event, "data": data}));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
|
@ -13,6 +13,11 @@ var loader_html = '<div class="preloader"> \
|
|||
</div> \
|
||||
</div>';
|
||||
|
||||
|
||||
/*
|
||||
* On initial document load events
|
||||
*/
|
||||
|
||||
$(document).ready(function () {
|
||||
remove_init_form();
|
||||
|
||||
|
@ -31,7 +36,7 @@ $(document).ready(function () {
|
|||
}
|
||||
}
|
||||
});
|
||||
}, 500);
|
||||
}, 100);
|
||||
});
|
||||
|
||||
/*
|
||||
|
@ -41,6 +46,15 @@ $(document).ready(function () {
|
|||
$(document).on('on_pjax_click', function (e, href) {
|
||||
$('li.active').removeClass("active");
|
||||
href.parent('li').addClass("active");
|
||||
|
||||
/* Unhook from all websocket events */
|
||||
websockets_unsubscribe_all_events();
|
||||
|
||||
/* Stream dashboard stats */
|
||||
if(document.location.pathname == "/"){
|
||||
websockets_subscribe_event("dhcp_statistics");
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
/*
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
function log_action (action) {
|
||||
switch (action) {
|
||||
case "stop":
|
||||
socket.send(JSON.stringify({"event_unsubscribe": "dhcp_log_subscription"}));
|
||||
websockets_unsubscribe_event("dhcp_log_subscription");
|
||||
break;
|
||||
case "start":
|
||||
killed_connection = 0;
|
||||
console.log('start readystate is ' + socket.readyState);
|
||||
socket.send(JSON.stringify({"event_subscription": "dhcp_log_subscription"}));
|
||||
websockets_subscribe_event("dhcp_log_subscription");
|
||||
break;
|
||||
case "clear":
|
||||
editor.setValue("");
|
||||
|
@ -53,4 +53,57 @@ function get_mac_oui_data() {
|
|||
mac_oui_data = data;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function parse_log_stream (console_data){
|
||||
|
||||
if (!document.getElementById("dhcp_log")) {
|
||||
console.log("[Websocket] DHCP Log unsubscribed");
|
||||
socket.send(JSON.stringify({"event_unsubscribe": "dhcp_log_subscription"}));
|
||||
killed_connection = 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (typeof mac_oui_data !== "undefined") {
|
||||
if (console_data.split(":").length - 1 >= 8) {
|
||||
var line_data = console_data.split(" ");
|
||||
for (i = 0; i < line_data.length; i++) {
|
||||
if ((line_data[i].split(":").length - 1) == 5) {
|
||||
var mac_oui = line_data[i].split(":").join("").toUpperCase().slice(0, 6);
|
||||
console_data = console_data.replace(line_data[i], line_data[i] + " (" + mac_oui_data[mac_oui] + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
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;
|
||||
|
||||
if (grep_value) {
|
||||
var matcher = new RegExp(grep_value, "i");
|
||||
var found = matcher.test(console_data);
|
||||
if (!found && !console_data.includes(grep_value)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
var session = editor.session;
|
||||
session.insert({
|
||||
row: session.getLength(),
|
||||
column: 0
|
||||
}, "\n" + console_data);
|
||||
|
||||
if (session.getLength() >= 50000) {
|
||||
/* If we get over 500,000 lines lets clear the editor */
|
||||
editor.setValue("");
|
||||
}
|
||||
|
||||
var row = editor.session.getLength() - 1;
|
||||
var column = editor.session.getLine(row).length; // or simply Infinity
|
||||
editor.gotoLine(row + 1, column);
|
||||
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
var subscribed_events = [];
|
||||
|
||||
function check_websocket_connection () {
|
||||
if (socket.readyState == 3) {
|
||||
connect_websocket();
|
||||
|
@ -5,8 +7,22 @@ function check_websocket_connection () {
|
|||
}
|
||||
}
|
||||
|
||||
function websockets_subscribe_event(event){
|
||||
socket.send(JSON.stringify({"event_subscription": event}));
|
||||
subscribed_events[event] = true;
|
||||
}
|
||||
|
||||
function websockets_unsubscribe_event(event){
|
||||
socket.send(JSON.stringify({"event_unsubscribe": event}));
|
||||
delete subscribed_events[event];
|
||||
}
|
||||
|
||||
function websockets_unsubscribe_all_events(){
|
||||
socket.send(JSON.stringify({"event_unsubscribe": "all_events"}));
|
||||
subscribed_events = [];
|
||||
}
|
||||
|
||||
function connect_websocket() {
|
||||
killed_connection = 0;
|
||||
|
||||
delete socket;
|
||||
socket = new WebSocket("ws://" + window.location.hostname + ":8080");
|
||||
|
@ -15,61 +31,21 @@ function connect_websocket() {
|
|||
console.log("[Websocket] socket is opened - readystate is " + socket.readyState);
|
||||
};
|
||||
|
||||
socket.onmessage = function (event) {
|
||||
if (killed_connection)
|
||||
return false;
|
||||
socket.onmessage = function (data) {
|
||||
|
||||
if (!document.getElementById("dhcp_log")) {
|
||||
console.log("[Websocket] DHCP Log unsubscribed");
|
||||
socket.send(JSON.stringify({"event_unsubscribe": "dhcp_log_subscription"}));
|
||||
killed_connection = 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
console_data = event.data;
|
||||
|
||||
if (typeof mac_oui_data !== "undefined") {
|
||||
if (console_data.split(":").length - 1 >= 8) {
|
||||
var line_data = console_data.split(" ");
|
||||
for (i = 0; i < line_data.length; i++) {
|
||||
if ((line_data[i].split(":").length - 1) == 5) {
|
||||
var mac_oui = line_data[i].split(":").join("").toUpperCase().slice(0, 6);
|
||||
console_data = console_data.replace(line_data[i], line_data[i] + " (" + mac_oui_data[mac_oui] + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var event_data = JSON.parse(data.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
|
||||
*/
|
||||
{event: "dhcp_log_subscription", data: "Oct 3 16:02:59 DHCP-Server dhcpd[5303]: reuse_l…% threshold, reply with unaltered, existing lease"}
|
||||
*/
|
||||
|
||||
var grep_value = document.getElementById("grep_fitler").value;
|
||||
if (!subscribed_events[event_data['event']])
|
||||
return false;
|
||||
|
||||
if (grep_value) {
|
||||
var matcher = new RegExp(grep_value, "i");
|
||||
var found = matcher.test(console_data);
|
||||
if (!found && !console_data.includes(grep_value)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/* Event Hooks */
|
||||
if (event_data['event'] == 'dhcp_log_subscription')
|
||||
parse_log_stream (event_data.data);
|
||||
|
||||
var session = editor.session;
|
||||
session.insert({
|
||||
row: session.getLength(),
|
||||
column: 0
|
||||
}, "\n" + console_data);
|
||||
|
||||
if (session.getLength() >= 50000) {
|
||||
/* If we get over 500,000 lines lets clear the editor */
|
||||
editor.setValue("");
|
||||
}
|
||||
|
||||
var row = editor.session.getLength() - 1;
|
||||
var column = editor.session.getLine(row).length; // or simply Infinity
|
||||
editor.gotoLine(row + 1, column);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -277,12 +277,20 @@
|
|||
|
||||
change_favicon("images/glass_logo.png");
|
||||
|
||||
get_stats_timer = setInterval(function () {
|
||||
var get_stats_timer = setInterval(function () {
|
||||
if (window_focus && $('#total-leases').length) {
|
||||
get_stats();
|
||||
}
|
||||
}, 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);
|
||||
|
||||
var window_focus = true;
|
||||
|
||||
|
|
Loading…
Reference in New Issue