Skip to content

Commit

Permalink
Merge pull request #66 from ratchetphp/run-once
Browse files Browse the repository at this point in the history
Prevent loop from being run on shutdown if it has already been run
  • Loading branch information
mbonneau committed May 22, 2018
2 parents 6cb895d + 38fbdd6 commit 85ca3b4
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Ratchet\Client;
use React\EventLoop\LoopInterface;
use React\EventLoop\Factory as ReactFactory;
use React\EventLoop\Timer\Timer;

/**
* @param string $url
Expand All @@ -16,8 +17,16 @@ function connect($url, array $subProtocols = [], $headers = [], LoopInterface $l
$connector = new Connector($loop);
$connection = $connector($url, $subProtocols, $headers);

register_shutdown_function(function() use ($loop) {
$loop->run();
$runHasBeenCalled = false;

$loop->addTimer(Timer::MIN_INTERVAL, function () use (&$runHasBeenCalled) {
$runHasBeenCalled = true;
});

register_shutdown_function(function() use ($loop, &$runHasBeenCalled) {
if (!$runHasBeenCalled) {
$loop->run();
}
});

return $connection;
Expand Down

0 comments on commit 85ca3b4

Please sign in to comment.