Skip to content

Use new Socket API and improve documentation and examples #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ Once [installed](#install), you can use the following code to access your local
Asterisk instance and issue some simple commands via AMI:

```php
<?php

require __DIR__ . '/vendor/autoload.php';

$factory = new Clue\React\Ami\Factory();

$factory->createClient('user:secret@localhost')->then(function (Clue\React\Ami\Client $client) {
Expand Down Expand Up @@ -118,7 +122,7 @@ proxy servers etc.), you can explicitly pass a custom instance of the
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):

```php
$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'dns' => '127.0.0.1',
'tcp' => array(
'bindto' => '192.168.10.1:0'
Expand Down Expand Up @@ -546,7 +550,7 @@ This is a shortcut to get the value of the "Event" field.

## Install

The recommended way to install this library is [through Composer](https://getcomposer.org).
The recommended way to install this library is [through Composer](https://getcomposer.org/).
[New to Composer?](https://getcomposer.org/doc/00-intro.md)

This project follows [SemVer](https://semver.org/).
Expand All @@ -560,12 +564,12 @@ See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.

This project aims to run on any platform and thus does not require any PHP
extensions and supports running on legacy PHP 5.3 through current PHP 8+.
It's *highly recommended to use PHP 7+* for this project.
It's *highly recommended to use the latest supported PHP version* for this project.

## Tests

To run the test suite, you first need to clone this repo and then install all
dependencies [through Composer](https://getcomposer.org):
dependencies [through Composer](https://getcomposer.org/):

```bash
$ composer install
Expand All @@ -574,7 +578,7 @@ $ composer install
To run the test suite, go to the project root and run:

```bash
$ php vendor/bin/phpunit
$ vendor/bin/phpunit
```

The test suite contains both unit tests and functional integration tests.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
"react/event-loop": "^1.2",
"react/promise": "^2.0 || ^1.1",
"react/socket": "^1.8"
"react/socket": "^1.9"
},
"require-dev": {
"clue/block-react": "^1.2",
Expand Down
18 changes: 8 additions & 10 deletions examples/commands.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
<?php

use Clue\React\Ami\Factory;
use Clue\React\Ami\Client;
use Clue\React\Ami\ActionSender;
use Clue\React\Ami\Protocol\Response;
use React\EventLoop\Loop;

require __DIR__ . '/../vendor/autoload.php';

$factory = new Factory();
$factory = new Clue\React\Ami\Factory();

$target = isset($argv[1]) ? $argv[1] : 'name:password@localhost';

$factory->createClient($target)->then(function (Client $client) {
$factory->createClient($target)->then(function (Clue\React\Ami\Client $client) {
echo 'Client connected. Use STDIN to send CLI commands via asterisk AMI.' . PHP_EOL;
$sender = new ActionSender($client);
$sender = new Clue\React\Ami\ActionSender($client);

$sender->events(false);

$sender->listCommands()->then(function (Response $response) {
$sender->listCommands()->then(function (Clue\React\Ami\Protocol\Response $response) {
echo 'Commands: ' . implode(', ', array_keys($response->getFields())) . PHP_EOL;
});

Expand All @@ -33,12 +29,14 @@
echo '<' . $line . PHP_EOL;

$sender->command($line)->then(
function (Response $response) {
function (Clue\React\Ami\Protocol\Response $response) {
echo $response->getCommandOutput() . PHP_EOL;
},
function (Exception $error) use ($line) {
echo 'Error executing "' . $line . '": ' . $error->getMessage() . PHP_EOL;
}
);
});
}, 'var_dump');
}, function (Exception $error) {
echo 'Connection error: ' . $error;
});
14 changes: 4 additions & 10 deletions examples/events.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
<?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';

$factory = new Factory();
$factory = new Clue\React\Ami\Factory();

$target = isset($argv[1]) ? $argv[1] : 'name:password@localhost';

$factory->createClient($target)->then(
function (Client $client) {
function (Clue\React\Ami\Client $client) {
echo 'Client connected ' . PHP_EOL;

$sender = new ActionSender($client);
$sender = new Clue\React\Ami\ActionSender($client);
$sender->events(true);

$client->on('close', function() {
echo 'Connection closed' . PHP_EOL;
});

$client->on('event', function (Event $event) {
$client->on('event', function (Clue\React\Ami\Protocol\Event $event) {
echo 'Event: ' . $event->getName() . ': ' . json_encode($event->getFields()) . PHP_EOL;
});
},
Expand Down
17 changes: 7 additions & 10 deletions examples/peers.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
<?php

use Clue\React\Ami\ActionSender;
use Clue\React\Ami\Client;
use Clue\React\Ami\Factory;
use Clue\React\Ami\Protocol\Collection;

require __DIR__ . '/../vendor/autoload.php';

$factory = new Factory();
$factory = new Clue\React\Ami\Factory();

$target = isset($argv[1]) ? $argv[1] : 'name:password@localhost';

$factory->createClient($target)->then(function (Client $client) {
$factory->createClient($target)->then(function (Clue\React\Ami\Client $client) {
echo 'Successfully connected' . PHP_EOL;

$collector = new ActionSender($client);
$collector = new Clue\React\Ami\ActionSender($client);

$collector->sipPeers()->then(function (Collection $collection) {
$collector->sipPeers()->then(function (Clue\React\Ami\Protocol\Collection $collection) {
var_dump('result', $collection);
$peers = $collection->getEntryEvents();

echo 'found ' . count($peers) . ' peers' . PHP_EOL;
});
}, 'var_dump');
}, function (Exception $error) {
echo 'Connection error: ' . $error;
});
4 changes: 2 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* [`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
*
* ```php
* $connector = new React\Socket\Connector(null, array(
* $connector = new React\Socket\Connector(array(
* 'dns' => '127.0.0.1',
* 'tcp' => array(
* 'bindto' => '192.168.10.1:0'
Expand All @@ -46,7 +46,7 @@ class Factory
public function __construct(LoopInterface $loop = null, ConnectorInterface $connector = null)
{
if ($connector === null) {
$connector = new Connector($loop);
$connector = new Connector(array(), $loop);
}

$this->connector = $connector;
Expand Down