Skip to content

Commit

Permalink
Merge pull request #16 from utopia-php/feat-onerror-callback
Browse files Browse the repository at this point in the history
Implemented onError callbacks
  • Loading branch information
eldadfux authored Dec 20, 2021
2 parents f6d163d + 237eed7 commit 51fcb86
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor/
.phpunit*
.idea
104 changes: 92 additions & 12 deletions src/WebSocket/Server.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Utopia\WebSocket;

use Exception;
use Throwable;
use Utopia\WebSocket\Adapter;

/**
Expand All @@ -15,6 +17,14 @@
*/
class Server
{
/**
* Callbacks that will be executed when an error occurs
*
* @var array
*/
protected $errorCallbacks = [];


protected Adapter $adapter;

/**
Expand All @@ -32,7 +42,13 @@ public function __construct(Adapter $adapter)
*/
public function start(): void
{
$this->adapter->start();
try {
$this->adapter->start();
} catch(Throwable $error) {
foreach ($this->errorCallbacks as $errorCallback) {
$errorCallback($error, "start");
}
}
}

/**
Expand All @@ -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");
}
}
}

/**
Expand All @@ -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");
}
}
}

/**
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}
}

0 comments on commit 51fcb86

Please sign in to comment.