Skip to content

Commit 51fcb86

Browse files
authored
Merge pull request #16 from utopia-php/feat-onerror-callback
Implemented onError callbacks
2 parents f6d163d + 237eed7 commit 51fcb86

File tree

2 files changed

+93
-12
lines changed

2 files changed

+93
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor/
22
.phpunit*
3+
.idea

src/WebSocket/Server.php

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Utopia\WebSocket;
33

4+
use Exception;
5+
use Throwable;
46
use Utopia\WebSocket\Adapter;
57

68
/**
@@ -15,6 +17,14 @@
1517
*/
1618
class Server
1719
{
20+
/**
21+
* Callbacks that will be executed when an error occurs
22+
*
23+
* @var array
24+
*/
25+
protected $errorCallbacks = [];
26+
27+
1828
protected Adapter $adapter;
1929

2030
/**
@@ -32,7 +42,13 @@ public function __construct(Adapter $adapter)
3242
*/
3343
public function start(): void
3444
{
35-
$this->adapter->start();
45+
try {
46+
$this->adapter->start();
47+
} catch(Throwable $error) {
48+
foreach ($this->errorCallbacks as $errorCallback) {
49+
$errorCallback($error, "start");
50+
}
51+
}
3652
}
3753

3854
/**
@@ -41,7 +57,13 @@ public function start(): void
4157
*/
4258
public function shutdown(): void
4359
{
44-
$this->adapter->shutdown();
60+
try {
61+
$this->adapter->shutdown();
62+
} catch(Throwable $error) {
63+
foreach ($this->errorCallbacks as $errorCallback) {
64+
$errorCallback($error, "shutdown");
65+
}
66+
}
4567
}
4668

4769
/**
@@ -52,18 +74,30 @@ public function shutdown(): void
5274
*/
5375
public function send(array $connections, string $message): void
5476
{
55-
$this->adapter->send($connections, $message);
77+
try {
78+
$this->adapter->send($connections, $message);
79+
} catch(Throwable $error) {
80+
foreach ($this->errorCallbacks as $errorCallback) {
81+
$errorCallback($error, "send");
82+
}
83+
}
5684
}
5785

5886
/**
5987
* Closes a connection.
6088
* @param int $connection Connection ID.
6189
* @param int $code Close Code.
62-
* @return void
90+
* @return void
6391
*/
6492
public function close(int $connection, int $code): void
6593
{
66-
$this->adapter->close($connection, $code);
94+
try {
95+
$this->adapter->close($connection, $code);
96+
} catch(Throwable $error) {
97+
foreach ($this->errorCallbacks as $errorCallback) {
98+
$errorCallback($error, "close");
99+
}
100+
}
67101
}
68102

69103
/**
@@ -73,7 +107,13 @@ public function close(int $connection, int $code): void
73107
*/
74108
public function onStart(callable $callback): self
75109
{
76-
$this->adapter->onStart($callback);
110+
try {
111+
$this->adapter->onStart($callback);
112+
} catch(Throwable $error) {
113+
foreach ($this->errorCallbacks as $errorCallback) {
114+
$errorCallback($error, "onStart");
115+
}
116+
}
77117
return $this;
78118
}
79119

@@ -84,7 +124,14 @@ public function onStart(callable $callback): self
84124
*/
85125
public function onWorkerStart(callable $callback): self
86126
{
87-
$this->adapter->onWorkerStart($callback);
127+
try {
128+
$this->adapter->onWorkerStart($callback);
129+
} catch(Throwable $error) {
130+
foreach ($this->errorCallbacks as $errorCallback) {
131+
$errorCallback($error, "onWorkerStart");
132+
}
133+
}
134+
88135
return $this;
89136
}
90137

@@ -95,7 +142,14 @@ public function onWorkerStart(callable $callback): self
95142
*/
96143
public function onOpen(callable $callback): self
97144
{
98-
$this->adapter->onOpen($callback);
145+
try {
146+
$this->adapter->onOpen($callback);
147+
} catch(Throwable $error) {
148+
foreach ($this->errorCallbacks as $errorCallback) {
149+
$errorCallback($error, "onOpen");
150+
}
151+
}
152+
99153
return $this;
100154
}
101155

@@ -106,7 +160,14 @@ public function onOpen(callable $callback): self
106160
*/
107161
public function onMessage(callable $callback): self
108162
{
109-
$this->adapter->onMessage($callback);
163+
try {
164+
$this->adapter->onMessage($callback);
165+
} catch(Throwable $error) {
166+
foreach ($this->errorCallbacks as $errorCallback) {
167+
$errorCallback($error, "onMessage");
168+
}
169+
}
170+
110171
return $this;
111172
}
112173

@@ -117,17 +178,36 @@ public function onMessage(callable $callback): self
117178
*/
118179
public function onClose(callable $callback): self
119180
{
120-
$this->adapter->onClose($callback);
181+
try {
182+
$this->adapter->onClose($callback);
183+
} catch(Throwable $error) {
184+
foreach ($this->errorCallbacks as $errorCallback) {
185+
$errorCallback($error, "onClose");
186+
}
187+
}
188+
121189
return $this;
122190
}
123191

124192
/**
125193
* Returns all connections.
126-
* @param callable $callback
127-
* @return array
194+
* @param callable $callback
195+
* @return array
128196
*/
129197
public function getConnections(): array
130198
{
131199
return $this->adapter->getConnections();
132200
}
201+
202+
/**
203+
* Register callback. Will be executed when error occurs.
204+
* @param callable $callback
205+
* @param Throwable $error
206+
* @return self
207+
*/
208+
public function error(callable $callback): self
209+
{
210+
\array_push($this->errorCallbacks, $callback);
211+
return $this;
212+
}
133213
}

0 commit comments

Comments
 (0)