diff --git a/README.md b/README.md index 79d9642..ce8d31c 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,10 @@ After installing, you can now use it in your code: throw new \RuntimeException('Could not connect to Asterisk Management Interface.'); } + // // if you have a looping of command function + // // set allowTimeout flag to true + // $ami->allowTimeout(); + // $result contains the output from the command $result = $ami->command('core show channels'); diff --git a/src/Ami.php b/src/Ami.php index 7ca571e..312217a 100644 --- a/src/Ami.php +++ b/src/Ami.php @@ -93,6 +93,11 @@ class Ami */ public $port; + /** + * @var int Used in waitResponse function to prevent looping when true + */ + private $allowTimeout; + /** * Event Handlers * @@ -186,6 +191,17 @@ public function sendRequest($action, array $parameters = []) return $response; }//end sendRequest() + /** + * Set global allowTimeout flag + * it will prevent looping waitResponse function + * + * @param boolean $value + * @return void + */ + public function allowTimeout($value = true) + { + $this->allowTimeout = boolval($value); + }//end allowTimeout() /** * Wait for a response @@ -199,10 +215,12 @@ public function sendRequest($action, array $parameters = []) */ public function waitResponse($allowTimeout = false) { - if (!is_resource($this->socket)) { - return []; - } - + if (!is_resource($this->socket)) { + return []; + } + + $allowTimeout = $this->allowTimeout ?: $allowTimeout; + // make sure we haven't already timed out $info = stream_get_meta_data($this->socket); if (feof($this->socket) === true || $info['timed_out'] === true) { @@ -381,8 +399,8 @@ public function disconnect() $this->logoff(); } - if (is_resource($this->socket)) { - fclose($this->socket); + if (is_resource($this->socket)) { + fclose($this->socket); } }//end disconnect()