Skip to content

Commit e96eac4

Browse files
authored
Merge pull request #125 from clue-labs/examples
Restructure examples to ease getting started
2 parents f5afd07 + cb3a43a commit e96eac4

6 files changed

Lines changed: 124 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;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
$loop = Factory::create();
8+
9+
// connect to www.google.com:80 (blocking call!)
10+
// for illustration purposes only, should use react/socket instead
11+
$stream = stream_socket_client('tcp://www.google.com:80');
12+
if (!$stream) {
13+
exit(1);
14+
}
15+
stream_set_blocking($stream, false);
16+
17+
// send HTTP request
18+
fwrite($stream, "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n");
19+
20+
// wait for HTTP response
21+
$loop->addReadStream($stream, function ($stream) use ($loop) {
22+
$chunk = fread($stream, 64 * 1024);
23+
24+
// reading nothing means we reached EOF
25+
if ($chunk === '') {
26+
echo '[END]' . PHP_EOL;
27+
$loop->removeReadStream($stream);
28+
fclose($stream);
29+
return;
30+
}
31+
32+
echo $chunk;
33+
});
34+
35+
$loop->run();

examples/14-http-client-async.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
$loop = Factory::create();
8+
9+
// resolve hostname before establishing TCP/IP connection (resolving DNS is still blocking here)
10+
// for illustration purposes only, should use react/socket or react/dns instead!
11+
$ip = gethostbyname('www.google.com');
12+
if (ip2long($ip) === false) {
13+
echo 'Unable to resolve hostname' . PHP_EOL;
14+
exit(1);
15+
}
16+
17+
// establish TCP/IP connection (non-blocking)
18+
// for illustraction purposes only, should use react/socket instead!
19+
$stream = stream_socket_client('tcp://' . $ip . ':80', $errno, $errstr, null, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT);
20+
if (!$stream) {
21+
exit(1);
22+
}
23+
stream_set_blocking($stream, false);
24+
25+
// print progress every 10ms
26+
echo 'Connecting';
27+
$timer = $loop->addPeriodicTimer(0.01, function () {
28+
echo '.';
29+
});
30+
31+
// wait for connection success/error
32+
$loop->addWriteStream($stream, function ($stream) use ($loop, $timer) {
33+
$loop->removeWriteStream($stream);
34+
$loop->cancelTimer($timer);
35+
36+
// check for socket error (connection rejected)
37+
if (stream_socket_get_name($stream, true) === false) {
38+
echo '[unable to connect]' . PHP_EOL;
39+
exit(1);
40+
} else {
41+
echo '[connected]' . PHP_EOL;
42+
}
43+
44+
// send HTTP request
45+
fwrite($stream, "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n");
46+
47+
// wait for HTTP response
48+
$loop->addReadStream($stream, function ($stream) use ($loop) {
49+
$chunk = fread($stream, 64 * 1024);
50+
51+
// reading nothing means we reached EOF
52+
if ($chunk === '') {
53+
echo '[END]' . PHP_EOL;
54+
$loop->removeReadStream($stream);
55+
fclose($stream);
56+
return;
57+
}
58+
59+
echo $chunk;
60+
});
61+
});
62+
63+
$loop->run();

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)