/** * 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()); }); }
<?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();