/** * @depends testConnection * @param Client $client */ public function testActionSenderLogoffDisconnects(Client $client) { $sender = new ActionSender($client); $ret = $this->waitFor($sender->logoff()); /* @var $ret Response */ $this->assertFalse($client->isBusy()); //$client->on('close', $this->expectCallableOnce()); self::$loop->run(); return $client; }
/** * Connect to AMI and start emitting events. * * @param string $address Example uaername:password@localhost:5038 * @return \React\Promise\Promise */ public function connect($address) { $factory = new Factory($this->eventLoop); return $factory->createClient($address)->then(function (Client $client) { $this->amiClient = $client; $this->actionSender = new ActionSender($client); $this->actionSender->events(true); $client->on('close', function () { $this->logger->debug('AMI connection closed'); }); $client->on('event', function (Event $event) { $this->wsClient->emit($event->getName(), (array) $event); }); }, function (\Exception $e) { $this->logger->err('Connection error: ' . $e->getMessage()); }); }
public function createClient($address = null) { $parts = $this->parseUrl($address); $secure = isset($parts['schema']) && $parts['schema'] !== 'tcp'; $connector = $secure ? $this->secureConnector : $this->connector; $promise = $connector->create($parts['host'], $parts['port'])->then(function (Stream $stream) { return new Client($stream); }); if (isset($parts['user'])) { $promise = $promise->then(function (Client $client) use($parts, $secure) { $sender = new ActionSender($client); return $sender->login($parts['user'], $parts['pass'])->then(function ($response) use($client) { return $client; }, function ($error) use($client) { $client->close(); throw $error; }); }); } return $promise; }
/** * Create client. * * @param array $options * * @return \React\Promise\Promise */ public function create(array $options = []) { foreach (['host', 'port', 'username', 'secret'] as $key) { $options[$key] = Arr::get($options, $key, null); } $promise = $this->connector->create($options['host'], $options['port'])->then(function (Stream $stream) { return new Client($stream, new Parser()); }); if (!is_null($options['username'])) { $promise = $promise->then(function (Client $client) use($options) { $sender = new ActionSender($client); return $sender->login($options['username'], $options['secret'])->then(function () use($client) { return $client; }, function ($error) use($client) { $client->close(); throw $error; }); }, function ($error) { throw $error; }); } return $promise; }
<?php use Clue\React\Ami\Factory; use Clue\React\Ami\Client; use Clue\React\Ami\ActionSender; use Clue\React\Ami\Protocol\Response; require __DIR__ . '/../vendor/autoload.php'; $loop = React\EventLoop\Factory::create(); $factory = new Factory($loop); $target = isset($argv[1]) ? $argv[1] : 'name:password@localhost'; $factory->createClient($target)->then(function (Client $client) use($loop) { echo 'Client connected. Use STDIN to send CLI commands via asterisk AMI.' . PHP_EOL; $sender = new ActionSender($client); $sender->events(false); $sender->listCommands()->then(function (Response $response) { echo 'Commands: ' . implode(', ', array_keys($response->getFields())) . PHP_EOL; }); $client->on('close', function () use($loop) { echo 'Closed' . PHP_EOL; $loop->removeReadStream(STDIN); }); $loop->addReadStream(STDIN, function () use($sender) { $line = trim(fread(STDIN, 4096)); echo '<' . $line . PHP_EOL; $sender->command($line)->then(function (Response $response) { echo $response->getCommandOutput() . PHP_EOL; }, function (Exception $error) use($line) { echo 'Error executing "' . $line . '": ' . $error->getMessage() . PHP_EOL; }); }); }, 'var_dump');
<?php use Clue\React\Ami\Factory; use Clue\React\Ami\Client; use Clue\React\Ami\ActionSender; use Clue\React\Ami\Protocol\Response; use Clue\React\Ami\Protocol\Event; require __DIR__ . '/../vendor/autoload.php'; $loop = React\EventLoop\Factory::create(); $factory = new Factory($loop); $target = isset($argv[1]) ? $argv[1] : 'name:password@localhost'; $factory->createClient($target)->then(function (Client $client) use($loop) { echo 'Client connected ' . PHP_EOL; $sender = new ActionSender($client); $sender->events(true); $client->on('close', function () { echo 'Connection closed' . PHP_EOL; }); $client->on('event', function (Event $event) { echo 'Event: ' . $event->getName() . ': ' . json_encode($event->getFields()) . PHP_EOL; }); }, function (Exception $error) { echo 'Connection error: ' . $error; }); $loop->run();