Skip to content

Commit a73caf8

Browse files
authored
Merge pull request #136 from clue-labs/examples
Restructure examples to ease getting started
2 parents 626bf5b + 460a8aa commit a73caf8

File tree

10 files changed

+169
-59
lines changed

10 files changed

+169
-59
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ $tcpConnector->connect('127.0.0.1:80')->then(function (ConnectionInterface $conn
10651065
$loop->run();
10661066
```
10671067

1068-
See also the [first example](examples).
1068+
See also the [examples](examples).
10691069

10701070
Pending connection attempts can be cancelled by cancelling its pending promise like so:
10711071

@@ -1133,7 +1133,7 @@ $dnsConnector->connect('www.google.com:80')->then(function (ConnectionInterface
11331133
$loop->run();
11341134
```
11351135

1136-
See also the [first example](examples).
1136+
See also the [examples](examples).
11371137

11381138
Pending connection attempts can be cancelled by cancelling its pending promise like so:
11391139

@@ -1178,7 +1178,7 @@ $secureConnector->connect('www.google.com:443')->then(function (ConnectionInterf
11781178
$loop->run();
11791179
```
11801180

1181-
See also the [second example](examples).
1181+
See also the [examples](examples).
11821182

11831183
Pending connection attempts can be cancelled by cancelling its pending promise like so:
11841184

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
// Just start this server and connect to it. Everything you send to it will be
44
// sent back to you.
55
//
6-
// $ php examples/01-echo.php 8000
6+
// $ php examples/01-echo-server.php 8000
77
// $ telnet localhost 8000
88
//
99
// You can also run a secure TLS echo server like this:
1010
//
11-
// $ php examples/01-echo.php tls://127.0.0.1:8000 examples/localhost.pem
11+
// $ php examples/01-echo-server.php tls://127.0.0.1:8000 examples/localhost.pem
1212
// $ openssl s_client -connect localhost:8000
1313
//
1414
// You can also run a Unix domain socket (UDS) server like this:
1515
//
16-
// $ php examples/01-echo.php unix:///tmp/server.sock
16+
// $ php examples/01-echo-server.php unix:///tmp/server.sock
1717
// $ nc -U /tmp/server.sock
1818

1919
use React\EventLoop\Factory;

examples/03-http-server.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
// Simple HTTP server example (for illustration purposes only).
4+
// This shows how a plaintext TCP/IP connection is accepted to then receive an
5+
// application level protocol message (HTTP request) and reply with an
6+
// application level protocol message (HTTP response) in return.
7+
//
8+
// This example exists for illustraion purposes only. It does not actually
9+
// parse incoming HTTP requests, so you can actually send *anything* and will
10+
// still respond with a valid HTTP response.
11+
// Real applications should use react/http instead!
12+
//
13+
// Just start this server and send a request to it:
14+
//
15+
// $ php examples/03-http-server.php 8000
16+
// $ curl -v http://localhost:8000/
17+
// $ ab -n1000 -c10 http://localhost:8000/
18+
// $ docker run -it --rm --net=host jordi/ab ab -n1000 -c10 http://localhost:8000/
19+
//
20+
// You can also run a secure HTTPS echo server like this:
21+
//
22+
// $ php examples/03-http-server.php tls://127.0.0.1:8000 examples/localhost.pem
23+
// $ curl -v --insecure https://localhost:8000/
24+
// $ ab -n1000 -c10 https://localhost:8000/
25+
// $ docker run -it --rm --net=host jordi/ab ab -n1000 -c10 https://localhost:8000/
26+
//
27+
// You can also run a Unix domain socket (UDS) server like this:
28+
//
29+
// $ php examples/03-http-server.php unix:///tmp/server.sock
30+
// $ nc -U /tmp/server.sock
31+
32+
use React\EventLoop\Factory;
33+
use React\Socket\Server;
34+
use React\Socket\ConnectionInterface;
35+
36+
require __DIR__ . '/../vendor/autoload.php';
37+
38+
$loop = Factory::create();
39+
40+
$server = new Server(isset($argv[1]) ? $argv[1] : 0, $loop, array(
41+
'tls' => array(
42+
'local_cert' => isset($argv[2]) ? $argv[2] : (__DIR__ . '/localhost.pem')
43+
)
44+
));
45+
46+
$server->on('connection', function (ConnectionInterface $conn) {
47+
$conn->once('data', function () use ($conn) {
48+
$body = "<html><h1>Hello world!</h1></html>\r\n";
49+
$conn->end("HTTP/1.1 200 OK\r\nContent-Length: " . strlen($body) . "\r\nConnection: close\r\n\r\n" . $body);
50+
});
51+
});
52+
53+
$server->on('error', 'printf');
54+
55+
echo 'Listening on ' . strtr($server->getAddress(), array('tcp:' => 'http:', 'tls:' => 'https:')) . PHP_EOL;
56+
57+
$loop->run();

examples/11-http-client.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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. See also example #22 for proper URI parsing.
10+
//
11+
// $ php examples/11-http-client.php
12+
// $ php examples/11-http-client.php reactphp.org
13+
14+
use React\EventLoop\Factory;
15+
use React\Socket\Connector;
16+
use React\Socket\ConnectionInterface;
17+
18+
$host = isset($argv[1]) ? $argv[1] : 'www.google.com';
19+
20+
require __DIR__ . '/../vendor/autoload.php';
21+
22+
$loop = Factory::create();
23+
$connector = new Connector($loop);
24+
25+
$connector->connect($host. ':80')->then(function (ConnectionInterface $connection) use ($host) {
26+
$connection->on('data', function ($data) {
27+
echo $data;
28+
});
29+
$connection->on('close', function () {
30+
echo '[CLOSED]' . PHP_EOL;
31+
});
32+
33+
$connection->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");
34+
}, 'printf');
35+
36+
$loop->run();

examples/11-http.php

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

examples/12-https-client.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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. See also example #22 for proper URI parsing.
10+
//
11+
// $ php examples/12-https-client.php
12+
// $ php examples/12-https-client.php reactphp.org
13+
14+
use React\EventLoop\Factory;
15+
use React\Socket\Connector;
16+
use React\Socket\ConnectionInterface;
17+
18+
$host = isset($argv[1]) ? $argv[1] : 'www.google.com';
19+
20+
require __DIR__ . '/../vendor/autoload.php';
21+
22+
$loop = Factory::create();
23+
$connector = new Connector($loop);
24+
25+
$connector->connect('tls://' . $host . ':443')->then(function (ConnectionInterface $connection) use ($host) {
26+
$connection->on('data', function ($data) {
27+
echo $data;
28+
});
29+
$connection->on('close', function () {
30+
echo '[CLOSED]' . PHP_EOL;
31+
});
32+
33+
$connection->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");
34+
}, 'printf');
35+
36+
$loop->run();

examples/12-https.php

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
<?php
22

3+
// Simple plaintext TCP/IP and secure TLS client example that pipes console I/O.
4+
// This shows how a plaintext TCP/IP or secure TLS connection is established and
5+
// then everything you type on STDIN will be sent and everything the server
6+
// sends will be piped to your STDOUT.
7+
//
8+
// $ php examples/21-netcat-client.php www.google.com:80
9+
// $ php examples/21-netcat-client.php tls://www.google.com:443
10+
311
use React\EventLoop\Factory;
412
use React\Socket\Connector;
513
use React\Socket\ConnectionInterface;
@@ -8,6 +16,16 @@
816

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

19+
if (!defined('STDIN')) {
20+
echo 'STDIO streams require CLI SAPI' . PHP_EOL;
21+
exit(1);
22+
}
23+
24+
if (DIRECTORY_SEPARATOR === '\\') {
25+
fwrite(STDERR, 'Non-blocking console I/O not supported on Microsoft Windows' . PHP_EOL);
26+
exit(1);
27+
}
28+
1129
if (!isset($argv[1])) {
1230
fwrite(STDERR, 'Usage error: required argument <host:port>' . PHP_EOL);
1331
exit(1);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
<?php
22

3+
// Simple plaintext HTTP and secure HTTPS client example (for illustration purposes only).
4+
// This shows how an URI parameter can parsed to decide whether to establish
5+
// a plaintext TCP/IP or secure TLS connection and then send an
6+
// application level protocol message (HTTP).
7+
// Real applications should use react/http-client instead!
8+
//
9+
// Unlike examples #11 and #12, this example will actually take an optional URI
10+
// parameter and parse it to connect to the correct default port and use the
11+
// correct transport protocol.
12+
//
13+
// $ php examples/22-http-client.php
14+
// $ php examples/22-http-client.php https://reactphp.org/
15+
316
use React\EventLoop\Factory;
417
use React\Socket\ConnectionInterface;
518
use React\Socket\Connector;
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
// sent for each connection and will print the average throughput once the
55
// connection closes.
66
//
7-
// $ php examples/03-benchmark.php 8000
7+
// $ php examples/91-benchmark-server.php 8000
88
// $ telnet localhost 8000
99
// $ echo hello world | nc -N localhost 8000
1010
// $ dd if=/dev/zero bs=1M count=1000 | nc -N localhost 8000
1111
//
1212
// You can also run a secure TLS benchmarking server like this:
1313
//
14-
// $ php examples/03-benchmark.php tls://127.0.0.1:8000 examples/localhost.pem
14+
// $ php examples/91-benchmark-server.php tls://127.0.0.1:8000 examples/localhost.pem
1515
// $ openssl s_client -connect localhost:8000
1616
// $ echo hello world | openssl s_client -connect localhost:8000
1717
// $ dd if=/dev/zero bs=1M count=1000 | openssl s_client -connect localhost:8000
1818
//
1919
// You can also run a Unix domain socket (UDS) server benchmark like this:
2020
//
21-
// $ php examples/03-benchmark.php unix:///tmp/server.sock
21+
// $ php examples/91-benchmark-server.php unix:///tmp/server.sock
2222
// $ nc -N -U /tmp/server.sock
2323
// $ dd if=/dev/zero bs=1M count=1000 | nc -N -U /tmp/server.sock
2424

0 commit comments

Comments
 (0)