Skip to content

Commit 9a54b9a

Browse files
authored
Merge pull request #123 from clue-labs/examples
Restructure examples to ease getting started
2 parents 97269c1 + f62f6dc commit 9a54b9a

File tree

6 files changed

+125
-16
lines changed

6 files changed

+125
-16
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ install:
4646

4747
script:
4848
- vendor/bin/phpunit --coverage-text
49-
- time php examples/benchmark-throughput.php
49+
- time php examples/91-benchmark-throughput.php

examples/01-http.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
// Simple plaintext HTTP client example (for illustration purposes only).
4+
// This shows how a plaintext TCP/IP connection is established to then send an
5+
// application level protocol message (HTTP).
6+
// Real applications should use react/http-client instead!
7+
//
8+
// This simple example only accepts an optional host parameter to send the
9+
// request to.
10+
//
11+
// $ php examples/01-http.php
12+
// $ php examples/01-http.php reactphp.org
13+
14+
use React\EventLoop\Factory;
15+
use React\Stream\DuplexResourceStream;
16+
17+
require __DIR__ . '/../vendor/autoload.php';
18+
19+
$host = isset($argv[1]) ? $argv[1] : 'www.google.com';
20+
21+
// connect to tcp://www.google.com:80 (blocking call!)
22+
// for illustration purposes only, should use react/http-client or react/socket instead!
23+
$resource = stream_socket_client('tcp://' . $host . ':80');
24+
if (!$resource) {
25+
exit(1);
26+
}
27+
28+
$loop = Factory::create();
29+
$stream = new DuplexResourceStream($resource, $loop);
30+
31+
$stream->on('data', function ($chunk) {
32+
echo $chunk;
33+
});
34+
$stream->on('close', function () {
35+
echo '[CLOSED]' . PHP_EOL;
36+
});
37+
38+
$stream->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");
39+
40+
$loop->run();

examples/02-https.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
// Simple secure HTTPS client example (for illustration purposes only).
4+
// This shows how a secure TLS connection is established to then send an
5+
// application level protocol message (HTTP).
6+
// Real applications should use react/http-client instead!
7+
//
8+
// This simple example only accepts an optional host parameter to send the
9+
// request to.
10+
//
11+
// $ php examples/02-https.php
12+
// $ php examples/02-https.php reactphp.org
13+
14+
use React\EventLoop\Factory;
15+
use React\Stream\DuplexResourceStream;
16+
17+
require __DIR__ . '/../vendor/autoload.php';
18+
19+
$host = isset($argv[1]) ? $argv[1] : 'www.google.com';
20+
21+
// connect to tls://www.google.com:443 (blocking call!)
22+
// for illustration purposes only, should use react/http-client or react/socket instead!
23+
$resource = stream_socket_client('tls://' . $host . ':443');
24+
if (!$resource) {
25+
exit(1);
26+
}
27+
28+
$loop = Factory::create();
29+
$stream = new DuplexResourceStream($resource, $loop);
30+
31+
$stream->on('data', function ($chunk) {
32+
echo $chunk;
33+
});
34+
$stream->on('close', function () {
35+
echo '[CLOSED]' . PHP_EOL;
36+
});
37+
38+
$stream->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");
39+
40+
$loop->run();

examples/11-cat.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
// Simple example piping everything from STDIN to STDOUT.
4+
// This allows you to output everything you type on your keyboard or to redirect
5+
// the pipes to show contents of files and other streams.
6+
//
7+
// $ php examples/11-cat.php
8+
// $ php examples/11-cat.php < README.md
9+
// $ echo hello | php examples/11-cat.php
10+
11+
use React\EventLoop\Factory;
12+
use React\Stream\ReadableResourceStream;
13+
use React\Stream\WritableResourceStream;
14+
15+
require __DIR__ . '/../vendor/autoload.php';
16+
17+
if (DIRECTORY_SEPARATOR === '\\') {
18+
fwrite(STDERR, 'Non-blocking console I/O not supported on Microsoft Windows' . PHP_EOL);
19+
exit(1);
20+
}
21+
22+
$loop = Factory::create();
23+
24+
$stdout = new WritableResourceStream(STDOUT, $loop);
25+
$stdin = new ReadableResourceStream(STDIN, $loop);
26+
$stdin->pipe($stdout);
27+
28+
$loop->run();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
<?php
22

3+
// Benchmark to measure throughput performance piping an input stream to an output stream.
4+
// This allows you to get an idea of how fast stream processing with PHP can be
5+
// and also to play around with differnt types of input and output streams.
6+
//
7+
// This example accepts a number of parameters to control the timeout (-t 1),
8+
// the input file (-i /dev/zero) and the output file (-o /dev/null).
9+
//
10+
// $ php examples/91-benchmark-throughput.php
11+
// $ php examples/91-benchmark-throughput.php -t 10 -o zero.bin
12+
// $ php examples/91-benchmark-throughput.php -t 60 -i zero.bin
13+
314
require __DIR__ . '/../vendor/autoload.php';
415

16+
if (DIRECTORY_SEPARATOR === '\\') {
17+
fwrite(STDERR, 'Non-blocking console I/O not supported on Microsoft Windows' . PHP_EOL);
18+
exit(1);
19+
}
20+
521
$args = getopt('i:o:t:');
622
$if = isset($args['i']) ? $args['i'] : '/dev/zero';
723
$of = isset($args['o']) ? $args['o'] : '/dev/null';

examples/cat.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)