Skip to content

Commit 199195c

Browse files
committed
Make examples more robust and link to higher level abstractions
1 parent 7bc0172 commit 199195c

4 files changed

Lines changed: 26 additions & 7 deletions

File tree

examples/04-signals.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

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

5+
if (!defined('SIGINT')) {
6+
fwrite(STDERR, 'Not supported on your platform (ext-pcntl missing or Windows?)' . PHP_EOL);
7+
exit(1);
8+
}
9+
510
$loop = React\EventLoop\Factory::create();
611

712
$loop->addSignal(SIGINT, $func = function ($signal) use ($loop, &$func) {
813
echo 'Signal: ', (string)$signal, PHP_EOL;
914
$loop->removeSignal(SIGINT, $func);
1015
});
1116

17+
echo 'Listening for SIGINT. Use "kill -SIGINT ' . getmypid() . '" or CTRL+C' . PHP_EOL;
18+
1219
$loop->run();

examples/11-consume-stdin.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@
44

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

7-
if (stream_set_blocking(STDIN, false) !== true) {
8-
fwrite(STDERR, 'ERROR: Unable to set STDIN non-blocking' . PHP_EOL);
7+
if (!defined('STDIN') || stream_set_blocking(STDIN, false) !== true) {
8+
fwrite(STDERR, 'ERROR: Unable to set STDIN non-blocking (not CLI or Windows?)' . PHP_EOL);
99
exit(1);
1010
}
1111

1212
$loop = Factory::create();
1313

14+
// read everything from STDIN and report number of bytes
15+
// for illustration purposes only, should use react/stream instead
1416
$loop->addReadStream(STDIN, function ($stream) use ($loop) {
1517
$chunk = fread($stream, 64 * 1024);
1618

1719
// reading nothing means we reached EOF
1820
if ($chunk === '') {
1921
$loop->removeReadStream($stream);
22+
stream_set_blocking($stream, true);
23+
fclose($stream);
2024
return;
2125
}
2226

examples/12-generate-yes.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,22 @@
1010

1111
$loop = React\EventLoop\Factory::create();
1212

13-
$stdout = STDOUT;
14-
if (stream_set_blocking($stdout, false) !== true) {
15-
fwrite(STDERR, 'ERROR: Unable to set STDOUT non-blocking' . PHP_EOL);
13+
if (!defined('STDOUT') || stream_set_blocking(STDOUT, false) !== true) {
14+
fwrite(STDERR, 'ERROR: Unable to set STDOUT non-blocking (not CLI or Windows?)' . PHP_EOL);
1615
exit(1);
1716
}
1817

19-
$loop->addWriteStream($stdout, function () use ($loop, $stdout, &$data) {
18+
// write data to STDOUT whenever its write buffer accepts data
19+
// for illustrations purpose only, should use react/stream instead
20+
$loop->addWriteStream(STDOUT, function ($stdout) use ($loop, &$data) {
2021
// try to write data
2122
$r = fwrite($stdout, $data);
2223

2324
// nothing could be written despite being writable => closed
2425
if ($r === 0) {
2526
$loop->removeWriteStream($stdout);
2627
fclose($stdout);
28+
stream_set_blocking($stdout, true);
2729
fwrite(STDERR, 'Stopped because STDOUT closed' . PHP_EOL);
2830

2931
return;

examples/21-http-server.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44

55
$loop = React\EventLoop\Factory::create();
66

7+
// start TCP/IP server on localhost:8080
8+
// for illustration purposes only, should use react/socket instead
79
$server = stream_socket_server('tcp://127.0.0.1:8080');
8-
stream_set_blocking($server, 0);
10+
if (!$server) {
11+
exit(1);
12+
}
13+
stream_set_blocking($server, false);
914

15+
// wait for incoming connections on server socket
1016
$loop->addReadStream($server, function ($server) use ($loop) {
1117
$conn = stream_socket_accept($server);
1218
$data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n";

0 commit comments

Comments
 (0)