Skip to content

Commit d914432

Browse files
authored
Merge pull request #99 from clue-labs/examples
Add examples to ease getting started
2 parents 67d845d + d4892c6 commit d914432

File tree

6 files changed

+128
-0
lines changed

6 files changed

+128
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ $loop->addPeriodicTimer(5, function () {
5656
$loop->run();
5757
```
5858

59+
See also the [examples](examples).
60+
5961
## Usage
6062

6163
Typical applications use a single event loop which is created at the beginning

examples/01-timers.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
$loop = React\EventLoop\Factory::create();
6+
7+
$loop->addTimer(0.8, function () {
8+
echo 'world!' . PHP_EOL;
9+
});
10+
11+
$loop->addTimer(0.3, function () {
12+
echo 'hello ';
13+
});
14+
15+
$loop->run();

examples/02-periodic.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
$loop = React\EventLoop\Factory::create();
6+
7+
$timer = $loop->addPeriodicTimer(0.1, function () {
8+
echo 'tick!' . PHP_EOL;
9+
});
10+
11+
$loop->addTimer(1.0, function () use ($loop, $timer) {
12+
$loop->cancelTimer($timer);
13+
echo 'Done' . PHP_EOL;
14+
});
15+
16+
$loop->run();

examples/11-consume-stdin.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
if (stream_set_blocking(STDIN, false) !== true) {
8+
fwrite(STDERR, 'ERROR: Unable to set STDIN non-blocking' . PHP_EOL);
9+
exit(1);
10+
}
11+
12+
$loop = Factory::create();
13+
14+
$loop->addReadStream(STDIN, function ($stream) use ($loop) {
15+
$chunk = fread($stream, 64 * 1024);
16+
17+
// reading nothing means we reached EOF
18+
if ($chunk === '') {
19+
$loop->removeReadStream($stream);
20+
return;
21+
}
22+
23+
echo strlen($chunk) . ' bytes' . PHP_EOL;
24+
});
25+
26+
$loop->run();

examples/12-generate-yes.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
// data can be given as first argument or defaults to "y"
6+
$data = (isset($argv[1]) ? $argv[1] : 'y') . "\n";
7+
8+
// repeat data X times in order to fill around 200 KB
9+
$data = str_repeat($data, round(200000 / strlen($data)));
10+
11+
$loop = React\EventLoop\Factory::create();
12+
13+
$stdout = STDOUT;
14+
if (stream_set_blocking($stdout, false) !== true) {
15+
fwrite(STDERR, 'ERROR: Unable to set STDOUT non-blocking' . PHP_EOL);
16+
exit(1);
17+
}
18+
19+
$loop->addWriteStream($stdout, function () use ($loop, $stdout, &$data) {
20+
// try to write data
21+
$r = fwrite($stdout, $data);
22+
23+
// nothing could be written despite being writable => closed
24+
if ($r === 0) {
25+
$loop->removeWriteStream($stdout);
26+
fclose($stdout);
27+
fwrite(STDERR, 'Stopped because STDOUT closed' . PHP_EOL);
28+
29+
return;
30+
}
31+
32+
// implement a very simple ring buffer, unless everything has been written at once:
33+
// everything written in this iteration will be appended for next iteration
34+
if (isset($data[$r])) {
35+
$data = substr($data, $r) . substr($data, 0, $r);
36+
}
37+
});
38+
39+
$loop->run();

examples/21-http-server.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
$loop = React\EventLoop\Factory::create();
6+
7+
$server = stream_socket_server('tcp://127.0.0.1:8080');
8+
stream_set_blocking($server, 0);
9+
10+
$loop->addReadStream($server, function ($server) use ($loop) {
11+
$conn = stream_socket_accept($server);
12+
$data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n";
13+
$loop->addWriteStream($conn, function ($conn) use (&$data, $loop) {
14+
$written = fwrite($conn, $data);
15+
if ($written === strlen($data)) {
16+
fclose($conn);
17+
$loop->removeStream($conn);
18+
} else {
19+
$data = substr($data, $written);
20+
}
21+
});
22+
});
23+
24+
$loop->addPeriodicTimer(5, function () {
25+
$memory = memory_get_usage() / 1024;
26+
$formatted = number_format($memory, 3).'K';
27+
echo "Current memory usage: {$formatted}\n";
28+
});
29+
30+
$loop->run();

0 commit comments

Comments
 (0)