Skip to content

Commit

Permalink
Added callback for testability and extensibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
lotharthesavior committed Aug 5, 2024
1 parent ecc6ec9 commit e4a659d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ This package has the following options (showing its respective defaults):
*/
'onDisconnectCallback' => null,

/**
* Callback for Reconnection moment.
* Passed parameters:
* - \WebSocket\Client $client
* - int \Throwable $e
*
* @var ?callable
*/
'onReconnectionCallback' => null,

/**
* When positive, considered in seconds
*
Expand Down
15 changes: 15 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ class Client implements ClientInterface
*/
protected $onMessageCallback = null;

/**
* Callback for Reconnection moment.
*
* @var callable|null
*/
protected $onReconnectionCallback = null;

/**
* Connection timeout.
*
Expand Down Expand Up @@ -143,6 +150,14 @@ private function startConnection()

private function handleReconnection(Throwable $e)
{
if (is_callable($this->onReconnectionCallback)) {
call_user_func(
$this->onReconnectionCallback,
$this,
$e,
);
}

if (
$this->reconnect
&& (
Expand Down

0 comments on commit e4a659d

Please sign in to comment.