Skip to content

Commit 2ddae07

Browse files
committed
Update README.md
1 parent 4639a07 commit 2ddae07

1 file changed

Lines changed: 144 additions & 1 deletion

File tree

README.md

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Asterisk Telephony instance and issue some simple commands via AMI:
3333
```php
3434
$factory = new Factory($loop);
3535

36-
$factory->createClient('username:secret@localhost')->then(function (Client $client) {
36+
$factory->createClient('user:secret@localhost')->then(function (Client $client) {
3737
echo 'Client connected' . PHP_EOL;
3838

3939
$api = new Api($client);
@@ -48,6 +48,149 @@ $loop->run();
4848

4949
See also the [examples](example).
5050

51+
## Usage
52+
53+
### Factory
54+
55+
The `Factory` is responsible for creating your `Client` instance.
56+
It also registers everything with the main `EventLoop`.
57+
58+
```php
59+
$loop = \React\EventLoop\Factory::create();
60+
$factory = new Factory($loop);
61+
```
62+
63+
The `createClient($amiUrl)` method can be used to create a new `Client`.
64+
It helps with establishing a plain TCP/IP or secure SSL connection to the AMI
65+
and issuing an initial `login` action.
66+
67+
```php
68+
$factory->createClient('user:secret@localhost')->then(
69+
function (Client $client) {
70+
// client connected and authenticated
71+
},
72+
function (Exception $e) {
73+
// an error occured while trying to connect or authorize client
74+
}
75+
);
76+
```
77+
78+
> Note: The given $amiUrl *must* include a host, it *should* include a username and secret
79+
> and it *can* include a scheme (tcp/ssl) and port definition.
80+
81+
### Client
82+
83+
The `Client` is responsible for exchanging messages with the Asterisk Manager Interface
84+
and keeps track of pending actions.
85+
86+
The `on($eventName, $eventHandler)` method can be used to register a new event handler.
87+
Incoming events and errors will be forwarded to registered event handler callbacks:
88+
89+
```php
90+
$client->on('event', function (Event $event) {
91+
// process an incoming AMI event (see below)
92+
});
93+
$client->on('close', function () {
94+
// the connection to the AMI just closed
95+
});
96+
$client->on('error', function (Exception $e) {
97+
// and error has just been detected, the connection will terminate...
98+
});
99+
```
100+
101+
The `close()` method can be used to force-close the AMI connection and reject all pending actions.
102+
103+
The `end()` method can be used to soft-close the AMI connection once all pending actions are completed.
104+
105+
> Advanced: Creating `Action` objects, sending them via AMI and waiting for incoming
106+
> `Response` objects is usually hidden behind the `Api` interface.
107+
>
108+
> If you happen to need a custom or otherwise unsupported action, you can also do so manually
109+
> as follows. Consider filing a PR though :)
110+
>
111+
> The `createAction($name, $fields)` method can be used to construct a custom AMI action.
112+
> A unique ActionID will be added automatically (needed to match incoming responses).
113+
>
114+
> The `request(Action $action)` method can be used to queue the given messages to be sent via AMI
115+
> and wait for a `Response` object that matches its ActionID.
116+
117+
### Api
118+
119+
The `Api` wraps a given `Client` instance to provide a simple way to execute common actions.
120+
121+
```php
122+
$api = new Api($client);
123+
124+
$api->ping()->then(function (Response $response) {
125+
// response received for ping action
126+
});
127+
```
128+
129+
All public methods resemble their respective AMI actions.
130+
Listing all available actions is out of scope here, please refer to the class outline.
131+
132+
Sending actions is async (non-blocking), so you can actually send multiple action requests in parallel.
133+
The AMI will respond to each action with a `Response` object. The order is not guaranteed.
134+
Sending actions uses a Promise-based interface that makes it easy to react to when an action is *fulfilled*
135+
(i.e. either successfully resolved or rejected with an error):
136+
137+
```php
138+
$api->ping()->then(
139+
function (Response $response) {
140+
// response received for ping action
141+
},
142+
function (Exception $e) {
143+
// an error occured while executing the action
144+
145+
if ($e instanceof ErrorException) {
146+
// we received a valid error response (such as authorization error)
147+
$response = $e->getResponse();
148+
} else {
149+
// we did not receive a valid response (likely a transport issue)
150+
}
151+
}
152+
});
153+
```
154+
155+
> Advanced: Using the `Api` is not strictly necessary, but is the recommended way to execute common actions.
156+
>
157+
> If you happen to need a new or otherwise unsupported action, or additional arguments,
158+
> you can also do so manually. See the advanced `Client` usage above for details.
159+
> A PR that updates the `API` is very much appreciated :)
160+
161+
### Message
162+
163+
The `Message` is an abstract base class for the `Response`, `Action` and `Event` value objects.
164+
It provides a common interface for these three message types.
165+
166+
Each `Message` consists of any number of fields with each having a name and one or multiple values.
167+
Field names are matched case-insensitive. The interpretation of values is application specific.
168+
169+
The `getFieldValue($key)` method can be used to get the first value for the given field key.
170+
If no value was found, `null` is returned.
171+
172+
The `getFieldValues($key)` method can be used to get a list of all values for the given field key.
173+
If no value was found, an empty `array()` is returned.
174+
175+
The `getFields()` method can be used to get an array of all fields.
176+
177+
The `getActionId()` method is a shortcut for accessing the value of the "ActionID" field.
178+
179+
#### Response
180+
181+
The `Response` value object represents the incoming response received from the AMI.
182+
183+
#### Action
184+
185+
The `Action` value object represents an outgoing action messages to be sent to the AMI.
186+
187+
#### Event
188+
189+
The `Event` value object represents the incoming event received from the AMI.
190+
191+
The `getName()` method can be used to get the name of the event. This is a shortcut to
192+
get the value of the "Event" field.
193+
51194
## Install
52195

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

0 commit comments

Comments
 (0)