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..2fe7912 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, "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, "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, "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, "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, "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, "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, "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, "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, "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 error(callable $callback): self + { + \array_push($this->errorCallbacks, $callback); + return $this; + } }