From 917f82f617bda5a0b63b4c94f32cada43acc7fb0 Mon Sep 17 00:00:00 2001 From: Matej Baco Date: Wed, 24 Nov 2021 13:04:35 +0100 Subject: [PATCH 1/2] Implemented onError callbacks --- .gitignore | 1 + src/WebSocket/Server.php | 104 ++++++++++++++++++++++++++++++++++----- 2 files changed, 93 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 6b8d168..97e1ffc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor/ .phpunit* +.idea diff --git a/src/WebSocket/Server.php b/src/WebSocket/Server.php index 14c4c14..e2a0fcb 100644 --- a/src/WebSocket/Server.php +++ b/src/WebSocket/Server.php @@ -1,6 +1,8 @@ adapter->start(); + try { + $this->adapter->start(); + } catch(Throwable $error) { + foreach ($this->errorCallbacks as $errorCallback) { + $errorCallback($error, "utopiaWebsocket.start"); + } + } } /** @@ -41,7 +57,13 @@ public function start(): void */ public function shutdown(): void { - $this->adapter->shutdown(); + try { + $this->adapter->shutdown(); + } catch(Throwable $error) { + foreach ($this->errorCallbacks as $errorCallback) { + $errorCallback($error, "utopiaWebsocket.shutdown"); + } + } } /** @@ -52,18 +74,30 @@ public function shutdown(): void */ public function send(array $connections, string $message): void { - $this->adapter->send($connections, $message); + try { + $this->adapter->send($connections, $message); + } catch(Throwable $error) { + foreach ($this->errorCallbacks as $errorCallback) { + $errorCallback($error, "utopiaWebsocket.send"); + } + } } /** * Closes a connection. * @param int $connection Connection ID. * @param int $code Close Code. - * @return void + * @return void */ public function close(int $connection, int $code): void { - $this->adapter->close($connection, $code); + try { + $this->adapter->close($connection, $code); + } catch(Throwable $error) { + foreach ($this->errorCallbacks as $errorCallback) { + $errorCallback($error, "utopiaWebsocket.close"); + } + } } /** @@ -73,7 +107,13 @@ public function close(int $connection, int $code): void */ public function onStart(callable $callback): self { - $this->adapter->onStart($callback); + try { + $this->adapter->onStart($callback); + } catch(Throwable $error) { + foreach ($this->errorCallbacks as $errorCallback) { + $errorCallback($error, "utopiaWebsocket.onStart"); + } + } return $this; } @@ -84,7 +124,14 @@ public function onStart(callable $callback): self */ public function onWorkerStart(callable $callback): self { - $this->adapter->onWorkerStart($callback); + try { + $this->adapter->onWorkerStart($callback); + } catch(Throwable $error) { + foreach ($this->errorCallbacks as $errorCallback) { + $errorCallback($error, "utopiaWebsocket.onWorkerStart"); + } + } + return $this; } @@ -95,7 +142,14 @@ public function onWorkerStart(callable $callback): self */ public function onOpen(callable $callback): self { - $this->adapter->onOpen($callback); + try { + $this->adapter->onOpen($callback); + } catch(Throwable $error) { + foreach ($this->errorCallbacks as $errorCallback) { + $errorCallback($error, "utopiaWebsocket.onOpen"); + } + } + return $this; } @@ -106,7 +160,14 @@ public function onOpen(callable $callback): self */ public function onMessage(callable $callback): self { - $this->adapter->onMessage($callback); + try { + $this->adapter->onMessage($callback); + } catch(Throwable $error) { + foreach ($this->errorCallbacks as $errorCallback) { + $errorCallback($error, "utopiaWebsocket.onMessage"); + } + } + return $this; } @@ -117,17 +178,36 @@ public function onMessage(callable $callback): self */ public function onClose(callable $callback): self { - $this->adapter->onClose($callback); + try { + $this->adapter->onClose($callback); + } catch(Throwable $error) { + foreach ($this->errorCallbacks as $errorCallback) { + $errorCallback($error, "utopiaWebsocket.onClose"); + } + } + return $this; } /** * Returns all connections. - * @param callable $callback - * @return array + * @param callable $callback + * @return array */ public function getConnections(): array { return $this->adapter->getConnections(); } + + /** + * Register callback. Will be executed when error occurs. + * @param callable $callback + * @param Throwable $error + * @return self + */ + public function onError(callable $callback): self + { + \array_push($this->errorCallbacks, $callback); + return $this; + } } From 237eed7e9c54a514cfabac3e5726d24661254f2e Mon Sep 17 00:00:00 2001 From: Matej Baco Date: Mon, 29 Nov 2021 09:43:24 +0100 Subject: [PATCH 2/2] PR review changes --- src/WebSocket/Server.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/WebSocket/Server.php b/src/WebSocket/Server.php index e2a0fcb..2fe7912 100644 --- a/src/WebSocket/Server.php +++ b/src/WebSocket/Server.php @@ -46,7 +46,7 @@ public function start(): void $this->adapter->start(); } catch(Throwable $error) { foreach ($this->errorCallbacks as $errorCallback) { - $errorCallback($error, "utopiaWebsocket.start"); + $errorCallback($error, "start"); } } } @@ -61,7 +61,7 @@ public function shutdown(): void $this->adapter->shutdown(); } catch(Throwable $error) { foreach ($this->errorCallbacks as $errorCallback) { - $errorCallback($error, "utopiaWebsocket.shutdown"); + $errorCallback($error, "shutdown"); } } } @@ -78,7 +78,7 @@ public function send(array $connections, string $message): void $this->adapter->send($connections, $message); } catch(Throwable $error) { foreach ($this->errorCallbacks as $errorCallback) { - $errorCallback($error, "utopiaWebsocket.send"); + $errorCallback($error, "send"); } } } @@ -95,7 +95,7 @@ public function close(int $connection, int $code): void $this->adapter->close($connection, $code); } catch(Throwable $error) { foreach ($this->errorCallbacks as $errorCallback) { - $errorCallback($error, "utopiaWebsocket.close"); + $errorCallback($error, "close"); } } } @@ -111,7 +111,7 @@ public function onStart(callable $callback): self $this->adapter->onStart($callback); } catch(Throwable $error) { foreach ($this->errorCallbacks as $errorCallback) { - $errorCallback($error, "utopiaWebsocket.onStart"); + $errorCallback($error, "onStart"); } } return $this; @@ -128,7 +128,7 @@ public function onWorkerStart(callable $callback): self $this->adapter->onWorkerStart($callback); } catch(Throwable $error) { foreach ($this->errorCallbacks as $errorCallback) { - $errorCallback($error, "utopiaWebsocket.onWorkerStart"); + $errorCallback($error, "onWorkerStart"); } } @@ -146,7 +146,7 @@ public function onOpen(callable $callback): self $this->adapter->onOpen($callback); } catch(Throwable $error) { foreach ($this->errorCallbacks as $errorCallback) { - $errorCallback($error, "utopiaWebsocket.onOpen"); + $errorCallback($error, "onOpen"); } } @@ -164,7 +164,7 @@ public function onMessage(callable $callback): self $this->adapter->onMessage($callback); } catch(Throwable $error) { foreach ($this->errorCallbacks as $errorCallback) { - $errorCallback($error, "utopiaWebsocket.onMessage"); + $errorCallback($error, "onMessage"); } } @@ -182,7 +182,7 @@ public function onClose(callable $callback): self $this->adapter->onClose($callback); } catch(Throwable $error) { foreach ($this->errorCallbacks as $errorCallback) { - $errorCallback($error, "utopiaWebsocket.onClose"); + $errorCallback($error, "onClose"); } } @@ -205,7 +205,7 @@ public function getConnections(): array * @param Throwable $error * @return self */ - public function onError(callable $callback): self + public function error(callable $callback): self { \array_push($this->errorCallbacks, $callback); return $this;