Skip to content

Commit

Permalink
Merge pull request #7 from russelomua/master
Browse files Browse the repository at this point in the history
solution for way to pass $allowTimeout into waitResponse
  • Loading branch information
ofbeaton authored Sep 3, 2019
2 parents 0d46709 + 7da4d7a commit bf93b90
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
30 changes: 24 additions & 6 deletions src/Ami.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ class Ami
*/
public $port;

/**
* @var int Used in waitResponse function to prevent looping when true
*/
private $allowTimeout;

/**
* Event Handlers
*
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit bf93b90

Please sign in to comment.