mirror of
https://github.com/fusionpbx/fusionpbx.git
synced 2026-01-06 11:43:50 +00:00
Add listeners to base_websocket_system_service
This commit is contained in:
@@ -24,7 +24,20 @@ abstract class base_websocket_system_service extends service implements websocke
|
|||||||
|
|
||||||
private $timers;
|
private $timers;
|
||||||
|
|
||||||
//abstract protected function reload_settings(): void;
|
/**
|
||||||
|
* Array of topics and their callbacks
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $topics;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Array of listeners
|
||||||
|
* Listener is an array of socket and callback used to listen for events on the socket. When a listener is added,
|
||||||
|
* the socket is added to the array of listeners. When the socket is closed, the listener is removed from the
|
||||||
|
* array of listeners. When an event is received on the respective socket, the provided callback is called.
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $listeners;
|
||||||
|
|
||||||
protected static function display_version(): void {
|
protected static function display_version(): void {
|
||||||
echo "System Dashboard Service 1.0\n";
|
echo "System Dashboard Service 1.0\n";
|
||||||
@@ -69,10 +82,24 @@ abstract class base_websocket_system_service extends service implements websocke
|
|||||||
self::$websocket_host = $host;
|
self::$websocket_host = $host;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a socket listener
|
||||||
|
*
|
||||||
|
* @param $socket
|
||||||
|
* @param callable $callback
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function add_listener($socket, callable $callback): void {
|
||||||
|
$this->listeners[] = [$socket, $callback];
|
||||||
|
}
|
||||||
|
|
||||||
public function run(): int {
|
public function run(): int {
|
||||||
// set the timers property as an array
|
// set the timers property as an array
|
||||||
$this->timers = [];
|
$this->timers = [];
|
||||||
|
|
||||||
|
// Set the listeners property as an array
|
||||||
|
$this->listeners = [];
|
||||||
|
|
||||||
// re-read the config file to get any possible changes
|
// re-read the config file to get any possible changes
|
||||||
parent::$config->read();
|
parent::$config->read();
|
||||||
|
|
||||||
@@ -94,7 +121,9 @@ abstract class base_websocket_system_service extends service implements websocke
|
|||||||
$suppress_ws_message = false;
|
$suppress_ws_message = false;
|
||||||
|
|
||||||
while ($this->running) {
|
while ($this->running) {
|
||||||
$read = [];
|
// Get the array of sockets to read from
|
||||||
|
$listeners = array_column($this->listeners, 0);
|
||||||
|
|
||||||
// reconnect to websocket server
|
// reconnect to websocket server
|
||||||
if ($this->ws_client === null || !$this->ws_client->is_connected()) {
|
if ($this->ws_client === null || !$this->ws_client->is_connected()) {
|
||||||
// reconnect failed
|
// reconnect failed
|
||||||
@@ -105,7 +134,9 @@ abstract class base_websocket_system_service extends service implements websocke
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($this->ws_client !== null && $this->ws_client->is_connected()) {
|
if ($this->ws_client !== null && $this->ws_client->is_connected()) {
|
||||||
$read[] = $this->ws_client->socket();
|
// Combine the websocket client and the listeners into a single array
|
||||||
|
$read = array_merge($listeners, $this->ws_client->socket());
|
||||||
|
// Reset the suppress message flag
|
||||||
$suppress_ws_message = false;
|
$suppress_ws_message = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,6 +160,14 @@ abstract class base_websocket_system_service extends service implements websocke
|
|||||||
$this->handle_websocket_event($this->ws_client);
|
$this->handle_websocket_event($this->ws_client);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
// Other listeners
|
||||||
|
foreach ($this->listeners as $listener) {
|
||||||
|
if ($resource === $listener[0]) {
|
||||||
|
// Call the callback function provided by the add_listener function
|
||||||
|
call_user_func($listener[1]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -143,7 +182,7 @@ abstract class base_websocket_system_service extends service implements websocke
|
|||||||
$callable = $array['callable'];
|
$callable = $array['callable'];
|
||||||
// Call the callback and see if it returns a value for the next timer
|
// Call the callback and see if it returns a value for the next timer
|
||||||
$next_timer = call_user_func($callable);
|
$next_timer = call_user_func($callable);
|
||||||
if ($next_timer !== null && is_numeric($next_timer)) {
|
if (is_numeric($next_timer)) {
|
||||||
// Set the timer again when requested by called function returning a value
|
// Set the timer again when requested by called function returning a value
|
||||||
$this->set_timer($next_timer, $callable);
|
$this->set_timer($next_timer, $callable);
|
||||||
}
|
}
|
||||||
@@ -244,8 +283,8 @@ abstract class base_websocket_system_service extends service implements websocke
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows the service to register a callback so when the topic arrives the callable is called
|
* Allows the service to register a callback so when the topic arrives the callable is called
|
||||||
* @param type $topic
|
* @param string $topic
|
||||||
* @param type $callable
|
* @param callable $callable
|
||||||
*/
|
*/
|
||||||
protected function on_topic($topic, $callable) {
|
protected function on_topic($topic, $callable) {
|
||||||
if (!isset($this->topics[$topic])) {
|
if (!isset($this->topics[$topic])) {
|
||||||
|
|||||||
@@ -308,19 +308,19 @@ class websocket_message extends base_message {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function to respond with a connected message
|
* Helper function to respond with a connected message
|
||||||
* @param type $request_id
|
* @param string|int $request_id
|
||||||
* @return type
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function connected($request_id = '') {
|
public static function connected($request_id = ''): string {
|
||||||
return static::request_authentication($request_id);
|
return static::request_authentication($request_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function to respond with a authentication message
|
* Helper function to respond with a authentication message
|
||||||
* @param type $request_id
|
* @param string|int $request_id
|
||||||
* @return type
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function request_authentication($request_id = '') {
|
public static function request_authentication($request_id = ''): string {
|
||||||
$class = static::class;
|
$class = static::class;
|
||||||
return (new $class())
|
return (new $class())
|
||||||
->request_id($request_id)
|
->request_id($request_id)
|
||||||
@@ -334,12 +334,12 @@ class websocket_message extends base_message {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function to respond with a bad request message
|
* Helper function to respond with a bad request message
|
||||||
* @param type $request_id
|
* @param string|int $request_id
|
||||||
* @param type $service
|
* @param string $service
|
||||||
* @param type $topic
|
* @param string $topic
|
||||||
* @return type
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function request_is_bad($request_id = '', $service = '', $topic = '') {
|
public static function request_is_bad($request_id = '', string $service = '', string $topic = ''): string {
|
||||||
$class = static::class;
|
$class = static::class;
|
||||||
return (new $class())
|
return (new $class())
|
||||||
->request_id($request_id)
|
->request_id($request_id)
|
||||||
@@ -352,12 +352,12 @@ class websocket_message extends base_message {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function to respond with an authenticated message
|
* Helper function to respond with an authenticated message
|
||||||
* @param type $request_id
|
* @param string|int $request_id
|
||||||
* @param type $service
|
* @param string $service
|
||||||
* @param type $topic
|
* @param string $topic
|
||||||
* @return type
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function request_authenticated($request_id = '', $service = '', $topic = 'authenticated') {
|
public static function request_authenticated($request_id = '', string $service = '', string $topic = 'authenticated'): string {
|
||||||
$class = static::class;
|
$class = static::class;
|
||||||
return (new $class())
|
return (new $class())
|
||||||
->request_id($request_id)
|
->request_id($request_id)
|
||||||
@@ -371,12 +371,12 @@ class websocket_message extends base_message {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function to respond with an unauthorized request message
|
* Helper function to respond with an unauthorized request message
|
||||||
* @param type $request_id
|
* @param string|int $request_id
|
||||||
* @param type $service
|
* @param string $service
|
||||||
* @param type $topic
|
* @param string $topic
|
||||||
* @return type
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function request_unauthorized($request_id = '', $service = '', $topic = 'unauthorized') {
|
public static function request_unauthorized($request_id = '', string $service = '', string $topic = 'unauthorized'): string {
|
||||||
$class = static::class;
|
$class = static::class;
|
||||||
return (new $class())
|
return (new $class())
|
||||||
->request_id($request_id)
|
->request_id($request_id)
|
||||||
@@ -389,12 +389,12 @@ class websocket_message extends base_message {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function to respond with a forbidden message
|
* Helper function to respond with a forbidden message
|
||||||
* @param type $request_id
|
* @param string|int $request_id
|
||||||
* @param type $service
|
* @param string $service
|
||||||
* @param type $topic
|
* @param string $topic
|
||||||
* @return type
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function request_forbidden($request_id = '', $service = '', $topic = 'forbidden') {
|
public static function request_forbidden($request_id = '', string $service = '', string $topic = 'forbidden'): string {
|
||||||
$class = static::class;
|
$class = static::class;
|
||||||
return (new $class())
|
return (new $class())
|
||||||
->request_id($request_id)
|
->request_id($request_id)
|
||||||
@@ -411,7 +411,7 @@ class websocket_message extends base_message {
|
|||||||
* @return static|null Returns a new websocket_message object (or child object)
|
* @return static|null Returns a new websocket_message object (or child object)
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public static function create_from_json_message($websocket_message_json) {
|
public static function create_from_json_message($websocket_message_json): ?websocket_message {
|
||||||
if (empty($websocket_message_json)) {
|
if (empty($websocket_message_json)) {
|
||||||
// Nothing to do
|
// Nothing to do
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user