Skip to content

Restructure examples to ease getting started #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/04-signals.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

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) {
echo 'Signal: ', (string)$signal, PHP_EOL;
$loop->removeSignal(SIGINT, $func);
});

echo 'Listening for SIGINT. Use "kill -SIGINT ' . getmypid() . '" or CTRL+C' . PHP_EOL;

$loop->run();
8 changes: 6 additions & 2 deletions examples/11-consume-stdin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 6 additions & 4 deletions examples/12-generate-yes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@

$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);

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

return;
Expand Down
35 changes: 35 additions & 0 deletions examples/13-http-client-blocking.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use React\EventLoop\Factory;

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

$loop = Factory::create();

// connect to www.google.com:80 (blocking call!)
// for illustration purposes only, should use react/socket instead
$stream = stream_socket_client('tcp://www.google.com:80');
if (!$stream) {
exit(1);
}
stream_set_blocking($stream, false);

// 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();
63 changes: 63 additions & 0 deletions examples/14-http-client-async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

use React\EventLoop\Factory;

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

$loop = Factory::create();

// resolve hostname before establishing TCP/IP connection (resolving DNS is still blocking here)
// for illustration purposes only, should use react/socket or react/dns instead!
$ip = gethostbyname('www.google.com');
if (ip2long($ip) === false) {
echo 'Unable to resolve hostname' . PHP_EOL;
exit(1);
}

// establish TCP/IP connection (non-blocking)
// for illustraction purposes only, should use react/socket instead!
$stream = stream_socket_client('tcp://' . $ip . ':80', $errno, $errstr, null, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT);
if (!$stream) {
exit(1);
}
stream_set_blocking($stream, false);

// print progress every 10ms
echo 'Connecting';
$timer = $loop->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();
8 changes: 7 additions & 1 deletion examples/21-http-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
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