diff --git a/src/Client.php b/src/Client.php index 28bcd77..4f7a5e0 100644 --- a/src/Client.php +++ b/src/Client.php @@ -19,6 +19,7 @@ class Client extends EventEmitter { + /** * @var LoopInterface */ @@ -52,6 +53,11 @@ class Client extends EventEmitter private $heartbeat_interval; + /** + * @var bool + */ + public $use_exceptions; + private $state; const PORT_DEFAULT_HTTP = 80; @@ -83,6 +89,7 @@ public function __construct($uri, LoopInterface $loop, Resolver $resolver = null $this->resolver = $resolver; $this->state = self::STATE_CLOSED; $this->heartbeat_interval = null; + $this->use_exceptions = true; } public function connect() @@ -170,4 +177,17 @@ public function getHeartbeatInterval() return $this->heartbeat_interval; } + public function useExceptions() + { + return $this->use_exceptions; + } + + /** + * @param bool $use_exceptions + */ + public function setUseExceptions($use_exceptions) + { + $this->use_exceptions = $use_exceptions; + } + } diff --git a/src/Protocol/AbstractProtocol.php b/src/Protocol/AbstractProtocol.php index a30d78f..c4fae1c 100644 --- a/src/Protocol/AbstractProtocol.php +++ b/src/Protocol/AbstractProtocol.php @@ -11,7 +11,8 @@ use React\EventLoop\Timer\Timer; use React\Socket\ConnectionInterface; -abstract class AbstractProtocol implements ProtocolInterface { +abstract class AbstractProtocol implements ProtocolInterface +{ /** * @var Client @@ -28,7 +29,8 @@ abstract class AbstractProtocol implements ProtocolInterface { */ protected $heartbeat_timer; - public function __construct(Client $client, ConnectionInterface $stream) { + public function __construct(Client $client, ConnectionInterface $stream) + { $this->client = $client; $this->stream = $stream; @@ -41,7 +43,7 @@ public function __construct(Client $client, ConnectionInterface $stream) { $buffer .= $data; //If the handler returns true, was successfully processed and can empty buffer - if($that->onStreamData($buffer)) { + if ($that->onStreamData($buffer)) { $buffer = ''; } }); @@ -51,20 +53,23 @@ public function __construct(Client $client, ConnectionInterface $stream) { $client->setState(Client::STATE_CLOSED); }); - $this->client->on('connect', function(){ - if(null !== $this->client->getHeartbeatInterval()){ + $this->client->on('connect', function () { + if (null !== $this->client->getHeartbeatInterval()) { $this->startHeartbeat(); } }); } - public function startHeartbeat(){ + public function startHeartbeat() + { - $this->client->getLoop()->addTimer($this->client->getHeartbeatInterval(), function(){ + $this->client->getLoop()->addTimer($this->client->getHeartbeatInterval(), function () { //Set a new timeout (2 sec seems reasonable) - $this->heartbeat_timer = $this->client->getLoop()->addTimer(2, function(){ + $this->heartbeat_timer = $this->client->getLoop()->addTimer(2, function () { $this->stream->close(); - throw new ConnectionLostException(); + if ($this->client->useExceptions()) { + throw new ConnectionLostException(); + } }); $this->sendHeartbeat(); @@ -73,9 +78,10 @@ public function startHeartbeat(){ } - public function onHeartbeat(){ + public function onHeartbeat() + { - if(isset($this->heartbeat_timer)){ + if (isset($this->heartbeat_timer)) { $this->client->getLoop()->cancelTimer($this->heartbeat_timer); } @@ -83,6 +89,8 @@ public function onHeartbeat(){ } - public function sendHeartbeat() {} + public function sendHeartbeat() + { + } -} \ No newline at end of file +}