Skip to content

Commit 460a8aa

Browse files
committed
Restructure examples to ease getting started
1 parent e514db1 commit 460a8aa

File tree

8 files changed

+42
-20
lines changed

8 files changed

+42
-20
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;
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@
1212
//
1313
// Just start this server and send a request to it:
1414
//
15-
// $ php examples/04-http-server.php 8000
15+
// $ php examples/03-http-server.php 8000
1616
// $ curl -v http://localhost:8000/
1717
// $ ab -n1000 -c10 http://localhost:8000/
1818
// $ docker run -it --rm --net=host jordi/ab ab -n1000 -c10 http://localhost:8000/
1919
//
2020
// You can also run a secure HTTPS echo server like this:
2121
//
22-
// $ php examples/04-http-server.php tls://127.0.0.1:8000 examples/localhost.pem
22+
// $ php examples/03-http-server.php tls://127.0.0.1:8000 examples/localhost.pem
2323
// $ curl -v --insecure https://localhost:8000/
2424
// $ ab -n1000 -c10 https://localhost:8000/
2525
// $ docker run -it --rm --net=host jordi/ab ab -n1000 -c10 https://localhost:8000/
2626
//
2727
// You can also run a Unix domain socket (UDS) server like this:
2828
//
29-
// $ php examples/04-http-server.php unix:///tmp/server.sock
29+
// $ php examples/03-http-server.php unix:///tmp/server.sock
3030
// $ nc -U /tmp/server.sock
3131

3232
use React\EventLoop\Factory;
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,34 @@
33
// Simple plaintext HTTP client example (for illustration purposes only).
44
// This shows how a plaintext TCP/IP connection is established to then send an
55
// application level protocol message (HTTP).
6-
// Real applications should use react/http-client instead
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
713

814
use React\EventLoop\Factory;
915
use React\Socket\Connector;
1016
use React\Socket\ConnectionInterface;
1117

12-
$target = isset($argv[1]) ? $argv[1] : 'www.google.com:80';
18+
$host = isset($argv[1]) ? $argv[1] : 'www.google.com';
1319

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

1622
$loop = Factory::create();
1723
$connector = new Connector($loop);
1824

19-
$connector->connect($target)->then(function (ConnectionInterface $connection) use ($target) {
25+
$connector->connect($host. ':80')->then(function (ConnectionInterface $connection) use ($host) {
2026
$connection->on('data', function ($data) {
2127
echo $data;
2228
});
2329
$connection->on('close', function () {
2430
echo '[CLOSED]' . PHP_EOL;
2531
});
2632

27-
$connection->write("GET / HTTP/1.0\r\nHost: $target\r\n\r\n");
33+
$connection->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");
2834
}, 'printf');
2935

3036
$loop->run();
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,33 @@
44
// This shows how a secure TLS connection is established to then send an
55
// application level protocol message (HTTP).
66
// 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
713

814
use React\EventLoop\Factory;
915
use React\Socket\Connector;
1016
use React\Socket\ConnectionInterface;
1117

12-
$target = isset($argv[1]) ? $argv[1] : 'www.google.com:443';
18+
$host = isset($argv[1]) ? $argv[1] : 'www.google.com';
1319

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

1622
$loop = Factory::create();
1723
$connector = new Connector($loop);
1824

19-
$connector->connect('tls://' . $target)->then(function (ConnectionInterface $connection) use ($target) {
25+
$connector->connect('tls://' . $host . ':443')->then(function (ConnectionInterface $connection) use ($host) {
2026
$connection->on('data', function ($data) {
2127
echo $data;
2228
});
2329
$connection->on('close', function () {
2430
echo '[CLOSED]' . PHP_EOL;
2531
});
2632

27-
$connection->write("GET / HTTP/1.0\r\nHost: $target\r\n\r\n");
33+
$connection->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");
2834
}, 'printf');
2935

3036
$loop->run();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
// This shows how a plaintext TCP/IP or secure TLS connection is established and
55
// then everything you type on STDIN will be sent and everything the server
66
// 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
710

811
use React\EventLoop\Factory;
912
use React\Socket\Connector;
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
// This shows how an URI parameter can parsed to decide whether to establish
55
// a plaintext TCP/IP or secure TLS connection and then send an
66
// application level protocol message (HTTP).
7-
// Real applications should use react/http-client instead
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/
815

916
use React\EventLoop\Factory;
1017
use React\Socket\ConnectionInterface;
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)