Websockets (#7393)

* Initial commit of websockets

* Move app_menu to the active_calls websockets

* Fix hangup function

* Remove connection wait-state on web socket server so events can process

* Add timestamp and debug level to console for service debug output

* Remove debug exit

* Fix typo for ws_client instead of ws_server

* Update app_config.php

* Fix typo and remove empty function

* Remove call to empty function

* Fix the menu to point to the correct location

* Remove Logging Class

* Rename service file

* Rename service file

* Fix the in progress browser request

* Fix browser reload and implement 'active_calls' default values

* Add apply_filter function

* Create new permission_filter object

* In progress active calls now use filter

* Add invalid_uuid_exception class

* add event_key_filter to honor user permissions

* add and_link and or_link for filters

* Fix disconnected subscriber and add filters to honor permissions

* Add $key and $value for filter

* define a service name

* catch throwable instead of exception

* Add $key and $value for filter and allow returning null

* Update permission checks when loading page

* Add apply_filter function to honor subscriber permissions

* Add create_filter_chain_for function to honor subscriber permissions

* Add apply_filter function to honor subscriber permissions

* Add apply_filter function to honor subscriber permissions

* create interface to allow filterable payload

* create interface to define functions required for websocket services

* Pass in service class when creating a service token

* Allow key/name and return null for filter

* Adjust subscriber exceptions to return the ID of the subscriber

* Add event filter to filter chain

* Add command line options for ip and port for websockets and switch

* update service to use is_a syntax

* initial commit of base class for websockets system services

* initial commit of the system cpu status service

* remove extra line feed

* fix path on active_calls

* initial proof of concept for cpu status updated by websockets

* Allow returning null

* Use default settings to set the interval for cpu status broadcast

* Improve the CPU percent function for Linux systems

* Show more debug information

* Allow child processes to re-connect to the web socket service

* Fix websockets as plural instead of singular

* Add class name list-row

* Update active_calls.php

* Update active_calls.php

* Update websocket_client.js

* Update app_config.php

* Update app_menu.php

* Update debian-websockets.service

* Update debian-active_calls.service

---------

Co-authored-by: FusionPBX <markjcrane@gmail.com>
This commit is contained in:
frytimo
2025-06-24 16:07:57 -03:00
committed by GitHub
parent 86f0561e0c
commit d5286a12bc
44 changed files with 9419 additions and 22 deletions

View File

@@ -1,9 +1,9 @@
<?php
//includes files
//includes files
require_once dirname(__DIR__, 4) . "/resources/require.php";
//check permisions
//check permisions
require_once "resources/check_auth.php";
if (permission_exists('xml_cdr_view')) {
//access granted
@@ -13,19 +13,19 @@
exit;
}
//add multi-lingual support
//add multi-lingual support
$language = new text;
$text = $language->get($_SESSION['domain']['language']['code'], 'app/system');
//system cpu status
//system cpu status
echo "<div class='hud_box'>\n";
//set the row style class names
//set the row style class names
$c = 0;
$row_style["0"] = "row_style0";
$row_style["1"] = "row_style1";
//get the CPU details
//get the CPU details
if (stristr(PHP_OS, 'BSD') || stristr(PHP_OS, 'Linux')) {
$result = shell_exec('ps -A -o pcpu');
@@ -50,17 +50,88 @@
}
//show the content
//show the content
echo "<div class='hud_content' ".($dashboard_details_state == "disabled" ?: "onclick=\"$('#hud_system_cpu_status_details').slideToggle('fast'); toggle_grid_row_end('".$dashboard_name."')\"").">\n";
echo " <span class='hud_title'><a onclick=\"document.location.href='".PROJECT_PATH."/app/system/system.php'\">".$text['label-cpu_usage']."</a></span>\n";
//add half doughnut chart
if (!isset($dashboard_chart_type) || $dashboard_chart_type == "doughnut") {
?>
$token = (new token())->create($_SERVER['PHP_SELF']);
echo " <input id='token' type='hidden' name='" . $token['name'] . "' value='" . $token['hash'] . "'>\n";
subscriber::save_token($token, [system_dashboard_service::get_service_name()]);
//break the caching with version
$version = md5(file_get_contents(__DIR__, '/resources/javascript/websocket_client.js'));
//set script source
echo "<script src='/app/system/resources/javascript/websocket_client.js?v=$version'></script>\n";
//add half doughnut chart
if (!isset($dashboard_chart_type) || $dashboard_chart_type == "doughnut"): ?>
<div class='hud_chart' style='width: 175px;'><canvas id='system_cpu_status_chart'></canvas></div>
<script>
const system_cpu_status_chart = new Chart(
const authToken = {
name: "<?= $token['name']; ?>",
hash: "<?= $token['hash']; ?>"
}
const serviceName = '<?php echo system_dashboard_service::get_service_name(); ?>'
const cpuStatusTopic = '<?php echo system_dashboard_service::CPU_STATUS_TOPIC; ?>';
const dashboard_cpu_usage_chart_main_color = [
'<?php echo ($settings->get('theme', 'dashboard_cpu_usage_chart_main_color')[0] ?? '#03c04a'); ?>',
'<?php echo ($settings->get('theme', 'dashboard_cpu_usage_chart_main_color')[1] ?? '#ff9933'); ?>',
'<?php echo ($settings->get('theme', 'dashboard_cpu_usage_chart_main_color')[2] ?? '#ea4c46'); ?>'
];
function connectWebsocket() {
client = new ws_client(`wss://${window.location.hostname}/websockets/`, authToken);
client.ws.addEventListener("open", async () => {
try {
console.log('Connected');
console.log('Requesting authentication');
await client.request('authentication');
client.onEvent(cpuStatusTopic, updateCpuChart);
client.request(serviceName, cpuStatusTopic);
} catch (err) {
console.error("WS setup failed: ", err);
return;
}
});
client.ws.addEventListener("close", async () => {
console.warn("Websocket Disconnected");
});
}
function bindEventHandlers(client) {
client.onEvent(cpuStatusTopic, updateCpuChart);
}
function updateCpuChart(payload) {
let cpuPercent = payload.cpu_status;
const chart = window.system_cpu_status_chart;
if (!chart) return;
// Update chart data
cpuPercent = Math.round(cpuPercent);
chart.data.datasets[0].data = [cpuPercent, 100 - cpuPercent];
// Update color based on threshold
if (cpuPercent <= 60) {
chart.data.datasets[0].backgroundColor[0] = dashboard_cpu_usage_chart_main_color[0];
} else if (cpuPercent <= 80) {
chart.data.datasets[0].backgroundColor[0] = dashboard_cpu_usage_chart_main_color[1];
} else {
chart.data.datasets[0].backgroundColor[0] = dashboard_cpu_usage_chart_main_color[2];
}
chart.options.plugins.chart_number_2.text = cpuPercent;
chart.update();
}
window.system_cpu_status_chart = new Chart(
document.getElementById('system_cpu_status_chart').getContext('2d'),
{
type: 'doughnut',
@@ -80,7 +151,7 @@
'<?php echo ($settings->get('theme', 'dashboard_cpu_usage_chart_sub_color') ?? '#d4d4d4'); ?>'
],
borderColor: '<?php echo $settings->get('theme', 'dashboard_chart_border_color'); ?>',
borderWidth: '<?php echo $settings->get('theme', 'dashboard_chart_border_width'); ?>',
borderWidth: '<?php echo $settings->get('theme', 'dashboard_chart_border_width'); ?>'
}]
},
options: {
@@ -92,7 +163,7 @@
},
tooltip: {
yAlign: 'bottom',
displayColors: false,
displayColors: false
}
}
},
@@ -110,9 +181,11 @@
}]
}
);
connectWebsocket();
</script>
<?php
}
<?php endif; ?>
<?php
if ($dashboard_chart_type == "number") {
echo "<span class='hud_stat'>".round($percent_cpu)."%</span>";
}