Skip to content
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

Restructure examples to ease getting started #123

Merged
merged 3 commits into from
Nov 19, 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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ install:

script:
- vendor/bin/phpunit --coverage-text
- time php examples/benchmark-throughput.php
- time php examples/91-benchmark-throughput.php
40 changes: 40 additions & 0 deletions examples/01-http.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

// Simple plaintext HTTP client example (for illustration purposes only).
// This shows how a plaintext TCP/IP connection is established to then send an
// application level protocol message (HTTP).
// Real applications should use react/http-client instead!
//
// This simple example only accepts an optional host parameter to send the
// request to.
//
// $ php examples/01-http.php
// $ php examples/01-http.php reactphp.org

use React\EventLoop\Factory;
use React\Stream\DuplexResourceStream;

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

$host = isset($argv[1]) ? $argv[1] : 'www.google.com';

// connect to tcp://www.google.com:80 (blocking call!)
// for illustration purposes only, should use react/http-client or react/socket instead!
$resource = stream_socket_client('tcp://' . $host . ':80');
if (!$resource) {
exit(1);
}

$loop = Factory::create();
$stream = new DuplexResourceStream($resource, $loop);

$stream->on('data', function ($chunk) {
echo $chunk;
});
$stream->on('close', function () {
echo '[CLOSED]' . PHP_EOL;
});

$stream->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");

$loop->run();
40 changes: 40 additions & 0 deletions examples/02-https.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

// Simple secure HTTPS client example (for illustration purposes only).
// This shows how a secure TLS connection is established to then send an
// application level protocol message (HTTP).
// Real applications should use react/http-client instead!
//
// This simple example only accepts an optional host parameter to send the
// request to.
//
// $ php examples/02-https.php
// $ php examples/02-https.php reactphp.org

use React\EventLoop\Factory;
use React\Stream\DuplexResourceStream;

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

$host = isset($argv[1]) ? $argv[1] : 'www.google.com';

// connect to tls://www.google.com:443 (blocking call!)
// for illustration purposes only, should use react/http-client or react/socket instead!
$resource = stream_socket_client('tls://' . $host . ':443');
if (!$resource) {
exit(1);
}

$loop = Factory::create();
$stream = new DuplexResourceStream($resource, $loop);

$stream->on('data', function ($chunk) {
echo $chunk;
});
$stream->on('close', function () {
echo '[CLOSED]' . PHP_EOL;
});

$stream->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");

$loop->run();
28 changes: 28 additions & 0 deletions examples/11-cat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

// Simple example piping everything from STDIN to STDOUT.
// This allows you to output everything you type on your keyboard or to redirect
// the pipes to show contents of files and other streams.
//
// $ php examples/11-cat.php
// $ php examples/11-cat.php < README.md
// $ echo hello | php examples/11-cat.php

use React\EventLoop\Factory;
use React\Stream\ReadableResourceStream;
use React\Stream\WritableResourceStream;

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

if (DIRECTORY_SEPARATOR === '\\') {
fwrite(STDERR, 'Non-blocking console I/O not supported on Microsoft Windows' . PHP_EOL);
exit(1);
}

$loop = Factory::create();

$stdout = new WritableResourceStream(STDOUT, $loop);
$stdin = new ReadableResourceStream(STDIN, $loop);
$stdin->pipe($stdout);

$loop->run();
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
<?php

// Benchmark to measure throughput performance piping an input stream to an output stream.
// This allows you to get an idea of how fast stream processing with PHP can be
// and also to play around with differnt types of input and output streams.
//
// This example accepts a number of parameters to control the timeout (-t 1),
// the input file (-i /dev/zero) and the output file (-o /dev/null).
//
// $ php examples/91-benchmark-throughput.php
// $ php examples/91-benchmark-throughput.php -t 10 -o zero.bin
// $ php examples/91-benchmark-throughput.php -t 60 -i zero.bin

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

if (DIRECTORY_SEPARATOR === '\\') {
fwrite(STDERR, 'Non-blocking console I/O not supported on Microsoft Windows' . PHP_EOL);
exit(1);
}

$args = getopt('i:o:t:');
$if = isset($args['i']) ? $args['i'] : '/dev/zero';
$of = isset($args['o']) ? $args['o'] : '/dev/null';
Expand Down
15 changes: 0 additions & 15 deletions examples/cat.php

This file was deleted.