Skip to content

Commit

Permalink
Improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
yadaiio committed Feb 26, 2024
1 parent 63fad80 commit f094b73
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,21 @@ as defined in [RFC 6763](https://tools.ietf.org/html/rfc6763).
Once [installed](#install), you can use the following code to look up the address of a local domain name:

```php
$factory = new Factory();
<?php

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

$factory = new Clue\React\Mdns\Factory();
$resolver = $factory->createResolver();

$resolver->lookup('hostname.local')->then(function ($ip) {
echo 'Found: ' . $ip . PHP_EOL;
$resolver->resolve('hostname.local')->then(function ($ip) {
echo 'Found: ' . $ip . PHP_EOL;
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

See also the [examples](examples).
See also the [examples](examples/).

## Usage

Expand Down Expand Up @@ -83,7 +89,7 @@ Sending queries uses a [Promise](https://github.com/reactphp/promise)-based inte
(i.e. either successfully resolved or rejected with an error):

```php
$resolver->lookup($hostname)->then(
$resolver->resolve($hostname)->then(
function ($ip) {
// IP successfully resolved
},
Expand All @@ -105,12 +111,16 @@ you should look into also using [clue/reactphp-block](https://github.com/clue/re
The resulting blocking code could look something like this:

```php
<?php

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

use Clue\React\Block;

$factory = new Factory();
$factory = new Clue\React\Mdns\Factory();
$resolver = $factory->createResolver();

$promise = $resolver->lookup('me.local');
$promise = $resolver->resolve('me.local');

try {
$ip = Block\await($promise, $loop);
Expand All @@ -120,12 +130,12 @@ try {
}
```

Similarly, you can also process multiple lookups concurrently and await an array of results:
Similarly, you can also process multiple resolves concurrently and await an array of results:

```php
$promises = array(
$resolver->lookup('first.local'),
$resolver->lookup('second.local'),
$resolver->resolve('first.local'),
$resolver->resolve('second.local'),
);

$ips = Block\awaitAll($promises, $loop);
Expand Down
8 changes: 4 additions & 4 deletions examples/lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
$factory = new Clue\React\Mdns\Factory();
$mdns = $factory->createResolver();

$mdns->resolve($name)->then('e', 'e');

function e($v) {
$mdns->resolve($name)->then(function ($v) {
echo $v . PHP_EOL;
}
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

0 comments on commit f094b73

Please sign in to comment.