Skip to content

Commit 1790d6d

Browse files
committed
Add more examples
1 parent 7708eac commit 1790d6d

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,37 @@ $factory = new Factory($loop);
5050

5151
The `createSender()` method can be used to create a socket capable of sending outgoing multicast datagrams and receiving incoming unicast responses. It returns a [`Socket`](#socket) instance.
5252

53+
```php
54+
$socket = $factory->createSender();
55+
56+
// send a multicast message to everybody listening on the given address
57+
$socket->send('hello?', '224.10.20.30:4050');
58+
59+
// report incoming unicast replies
60+
$socket->on('message', function ($data, $address) {
61+
echo 'received ' . strlen($data) . ' bytes from ' . $address . PHP_EOL;
62+
});
63+
```
64+
5365
This method works on PHP versions as old as PHP 5.3 (and up), as its socket API has always been
5466
[level 1 multicast conformant](http://www.tldp.org/HOWTO/Multicast-HOWTO-2.html#ss2.2).
5567

5668
#### createReceiver()
5769

5870
The `createSender($address)` method can be used to create a socket capable of receiving incoming multicast datagrams and sending outgoing unicast or multicast datagrams. It returns a [`Socket`](#socket) instance.
5971

72+
```php
73+
$socket = $factory->createReceiver('224.10.20.30:4050');
74+
75+
// report incoming multicast messages
76+
$socket->on('message', function ($data, $remote) use ($socket) {
77+
echo 'Sending back ' . strlen($data) . ' bytes to ' . $remote . PHP_EOL;
78+
79+
// send a unitcast reply to the remote
80+
$socket->send($data, $remote);
81+
});
82+
```
83+
6084
This method requires PHP 5.4 (or up) and ext-sockets.
6185
Otherwise, it will throw a `BadMethodCallException`.
6286
This is a requirement because receiving multicast datagrams requires a

0 commit comments

Comments
 (0)