diff --git a/examples/04-signals.php b/examples/04-signals.php index 4e03e4b7..90b68989 100644 --- a/examples/04-signals.php +++ b/examples/04-signals.php @@ -2,6 +2,11 @@ require __DIR__ . '/../vendor/autoload.php'; +if (!defined('SIGINT')) { + fwrite(STDERR, 'Not supported on your platform (ext-pcntl missing or Windows?)' . PHP_EOL); + exit(1); +} + $loop = React\EventLoop\Factory::create(); $loop->addSignal(SIGINT, $func = function ($signal) use ($loop, &$func) { @@ -9,4 +14,6 @@ $loop->removeSignal(SIGINT, $func); }); +echo 'Listening for SIGINT. Use "kill -SIGINT ' . getmypid() . '" or CTRL+C' . PHP_EOL; + $loop->run(); diff --git a/examples/11-consume-stdin.php b/examples/11-consume-stdin.php index c1874dd3..2a772455 100644 --- a/examples/11-consume-stdin.php +++ b/examples/11-consume-stdin.php @@ -4,19 +4,23 @@ require __DIR__ . '/../vendor/autoload.php'; -if (stream_set_blocking(STDIN, false) !== true) { - fwrite(STDERR, 'ERROR: Unable to set STDIN non-blocking' . PHP_EOL); +if (!defined('STDIN') || stream_set_blocking(STDIN, false) !== true) { + fwrite(STDERR, 'ERROR: Unable to set STDIN non-blocking (not CLI or Windows?)' . PHP_EOL); exit(1); } $loop = Factory::create(); +// read everything from STDIN and report number of bytes +// for illustration purposes only, should use react/stream instead $loop->addReadStream(STDIN, function ($stream) use ($loop) { $chunk = fread($stream, 64 * 1024); // reading nothing means we reached EOF if ($chunk === '') { $loop->removeReadStream($stream); + stream_set_blocking($stream, true); + fclose($stream); return; } diff --git a/examples/12-generate-yes.php b/examples/12-generate-yes.php index bb78e88f..ebc2beb4 100644 --- a/examples/12-generate-yes.php +++ b/examples/12-generate-yes.php @@ -10,13 +10,14 @@ $loop = React\EventLoop\Factory::create(); -$stdout = STDOUT; -if (stream_set_blocking($stdout, false) !== true) { - fwrite(STDERR, 'ERROR: Unable to set STDOUT non-blocking' . PHP_EOL); +if (!defined('STDOUT') || stream_set_blocking(STDOUT, false) !== true) { + fwrite(STDERR, 'ERROR: Unable to set STDOUT non-blocking (not CLI or Windows?)' . PHP_EOL); exit(1); } -$loop->addWriteStream($stdout, function () use ($loop, $stdout, &$data) { +// write data to STDOUT whenever its write buffer accepts data +// for illustrations purpose only, should use react/stream instead +$loop->addWriteStream(STDOUT, function ($stdout) use ($loop, &$data) { // try to write data $r = fwrite($stdout, $data); @@ -24,6 +25,7 @@ if ($r === 0) { $loop->removeWriteStream($stdout); fclose($stdout); + stream_set_blocking($stdout, true); fwrite(STDERR, 'Stopped because STDOUT closed' . PHP_EOL); return; diff --git a/examples/13-http-client-blocking.php b/examples/13-http-client-blocking.php new file mode 100644 index 00000000..a2dde55c --- /dev/null +++ b/examples/13-http-client-blocking.php @@ -0,0 +1,35 @@ +addReadStream($stream, function ($stream) use ($loop) { + $chunk = fread($stream, 64 * 1024); + + // reading nothing means we reached EOF + if ($chunk === '') { + echo '[END]' . PHP_EOL; + $loop->removeReadStream($stream); + fclose($stream); + return; + } + + echo $chunk; +}); + +$loop->run(); diff --git a/examples/14-http-client-async.php b/examples/14-http-client-async.php new file mode 100644 index 00000000..c82c9887 --- /dev/null +++ b/examples/14-http-client-async.php @@ -0,0 +1,63 @@ +addPeriodicTimer(0.01, function () { + echo '.'; +}); + +// wait for connection success/error +$loop->addWriteStream($stream, function ($stream) use ($loop, $timer) { + $loop->removeWriteStream($stream); + $loop->cancelTimer($timer); + + // check for socket error (connection rejected) + if (stream_socket_get_name($stream, true) === false) { + echo '[unable to connect]' . PHP_EOL; + exit(1); + } else { + echo '[connected]' . PHP_EOL; + } + + // send HTTP request + fwrite($stream, "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n"); + + // wait for HTTP response + $loop->addReadStream($stream, function ($stream) use ($loop) { + $chunk = fread($stream, 64 * 1024); + + // reading nothing means we reached EOF + if ($chunk === '') { + echo '[END]' . PHP_EOL; + $loop->removeReadStream($stream); + fclose($stream); + return; + } + + echo $chunk; + }); +}); + +$loop->run(); diff --git a/examples/21-http-server.php b/examples/21-http-server.php index 66180de1..89520cec 100644 --- a/examples/21-http-server.php +++ b/examples/21-http-server.php @@ -4,9 +4,15 @@ $loop = React\EventLoop\Factory::create(); +// start TCP/IP server on localhost:8080 +// for illustration purposes only, should use react/socket instead $server = stream_socket_server('tcp://127.0.0.1:8080'); -stream_set_blocking($server, 0); +if (!$server) { + exit(1); +} +stream_set_blocking($server, false); +// wait for incoming connections on server socket $loop->addReadStream($server, function ($server) use ($loop) { $conn = stream_socket_accept($server); $data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n"; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy