Skip to content

Restructure examples to ease getting started #136

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

Merged
merged 3 commits into from
Nov 18, 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,7 @@ $tcpConnector->connect('127.0.0.1:80')->then(function (ConnectionInterface $conn
$loop->run();
```

See also the [first example](examples).
See also the [examples](examples).

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

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

See also the [first example](examples).
See also the [examples](examples).

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

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

See also the [second example](examples).
See also the [examples](examples).

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

Expand Down
6 changes: 3 additions & 3 deletions examples/01-echo.php → examples/01-echo-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
// Just start this server and connect to it. Everything you send to it will be
// sent back to you.
//
// $ php examples/01-echo.php 8000
// $ php examples/01-echo-server.php 8000
// $ telnet localhost 8000
//
// You can also run a secure TLS echo server like this:
//
// $ php examples/01-echo.php tls://127.0.0.1:8000 examples/localhost.pem
// $ php examples/01-echo-server.php tls://127.0.0.1:8000 examples/localhost.pem
// $ openssl s_client -connect localhost:8000
//
// You can also run a Unix domain socket (UDS) server like this:
//
// $ php examples/01-echo.php unix:///tmp/server.sock
// $ php examples/01-echo-server.php unix:///tmp/server.sock
// $ nc -U /tmp/server.sock

use React\EventLoop\Factory;
Expand Down
57 changes: 57 additions & 0 deletions examples/03-http-server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

// Simple HTTP server example (for illustration purposes only).
// This shows how a plaintext TCP/IP connection is accepted to then receive an
// application level protocol message (HTTP request) and reply with an
// application level protocol message (HTTP response) in return.
//
// This example exists for illustraion purposes only. It does not actually
// parse incoming HTTP requests, so you can actually send *anything* and will
// still respond with a valid HTTP response.
// Real applications should use react/http instead!
//
// Just start this server and send a request to it:
//
// $ php examples/03-http-server.php 8000
// $ curl -v http://localhost:8000/
// $ ab -n1000 -c10 http://localhost:8000/
// $ docker run -it --rm --net=host jordi/ab ab -n1000 -c10 http://localhost:8000/
//
// You can also run a secure HTTPS echo server like this:
//
// $ php examples/03-http-server.php tls://127.0.0.1:8000 examples/localhost.pem
// $ curl -v --insecure https://localhost:8000/
// $ ab -n1000 -c10 https://localhost:8000/
// $ docker run -it --rm --net=host jordi/ab ab -n1000 -c10 https://localhost:8000/
//
// You can also run a Unix domain socket (UDS) server like this:
//
// $ php examples/03-http-server.php unix:///tmp/server.sock
// $ nc -U /tmp/server.sock

use React\EventLoop\Factory;
use React\Socket\Server;
use React\Socket\ConnectionInterface;

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

$loop = Factory::create();

$server = new Server(isset($argv[1]) ? $argv[1] : 0, $loop, array(
'tls' => array(
'local_cert' => isset($argv[2]) ? $argv[2] : (__DIR__ . '/localhost.pem')
)
));

$server->on('connection', function (ConnectionInterface $conn) {
$conn->once('data', function () use ($conn) {
$body = "<html><h1>Hello world!</h1></html>\r\n";
$conn->end("HTTP/1.1 200 OK\r\nContent-Length: " . strlen($body) . "\r\nConnection: close\r\n\r\n" . $body);
});
});

$server->on('error', 'printf');

echo 'Listening on ' . strtr($server->getAddress(), array('tcp:' => 'http:', 'tls:' => 'https:')) . PHP_EOL;

$loop->run();
36 changes: 36 additions & 0 deletions examples/11-http-client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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. See also example #22 for proper URI parsing.
//
// $ php examples/11-http-client.php
// $ php examples/11-http-client.php reactphp.org

use React\EventLoop\Factory;
use React\Socket\Connector;
use React\Socket\ConnectionInterface;

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

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

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

$connector->connect($host. ':80')->then(function (ConnectionInterface $connection) use ($host) {
$connection->on('data', function ($data) {
echo $data;
});
$connection->on('close', function () {
echo '[CLOSED]' . PHP_EOL;
});

$connection->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");
}, 'printf');

$loop->run();
25 changes: 0 additions & 25 deletions examples/11-http.php

This file was deleted.

36 changes: 36 additions & 0 deletions examples/12-https-client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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. See also example #22 for proper URI parsing.
//
// $ php examples/12-https-client.php
// $ php examples/12-https-client.php reactphp.org

use React\EventLoop\Factory;
use React\Socket\Connector;
use React\Socket\ConnectionInterface;

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

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

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

$connector->connect('tls://' . $host . ':443')->then(function (ConnectionInterface $connection) use ($host) {
$connection->on('data', function ($data) {
echo $data;
});
$connection->on('close', function () {
echo '[CLOSED]' . PHP_EOL;
});

$connection->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");
}, 'printf');

$loop->run();
25 changes: 0 additions & 25 deletions examples/12-https.php

This file was deleted.

18 changes: 18 additions & 0 deletions examples/13-netcat.php → examples/21-netcat-client.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

// Simple plaintext TCP/IP and secure TLS client example that pipes console I/O.
// This shows how a plaintext TCP/IP or secure TLS connection is established and
// then everything you type on STDIN will be sent and everything the server
// sends will be piped to your STDOUT.
//
// $ php examples/21-netcat-client.php www.google.com:80
// $ php examples/21-netcat-client.php tls://www.google.com:443

use React\EventLoop\Factory;
use React\Socket\Connector;
use React\Socket\ConnectionInterface;
Expand All @@ -8,6 +16,16 @@

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

if (!defined('STDIN')) {
echo 'STDIO streams require CLI SAPI' . PHP_EOL;
exit(1);
}

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

if (!isset($argv[1])) {
fwrite(STDERR, 'Usage error: required argument <host:port>' . PHP_EOL);
exit(1);
Expand Down
13 changes: 13 additions & 0 deletions examples/14-web.php → examples/22-http-client.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
<?php

// Simple plaintext HTTP and secure HTTPS client example (for illustration purposes only).
// This shows how an URI parameter can parsed to decide whether to establish
// a plaintext TCP/IP or secure TLS connection and then send an
// application level protocol message (HTTP).
// Real applications should use react/http-client instead!
//
// Unlike examples #11 and #12, this example will actually take an optional URI
// parameter and parse it to connect to the correct default port and use the
// correct transport protocol.
//
// $ php examples/22-http-client.php
// $ php examples/22-http-client.php https://reactphp.org/

use React\EventLoop\Factory;
use React\Socket\ConnectionInterface;
use React\Socket\Connector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
// sent for each connection and will print the average throughput once the
// connection closes.
//
// $ php examples/03-benchmark.php 8000
// $ php examples/91-benchmark-server.php 8000
// $ telnet localhost 8000
// $ echo hello world | nc -N localhost 8000
// $ dd if=/dev/zero bs=1M count=1000 | nc -N localhost 8000
//
// You can also run a secure TLS benchmarking server like this:
//
// $ php examples/03-benchmark.php tls://127.0.0.1:8000 examples/localhost.pem
// $ php examples/91-benchmark-server.php tls://127.0.0.1:8000 examples/localhost.pem
// $ openssl s_client -connect localhost:8000
// $ echo hello world | openssl s_client -connect localhost:8000
// $ dd if=/dev/zero bs=1M count=1000 | openssl s_client -connect localhost:8000
//
// You can also run a Unix domain socket (UDS) server benchmark like this:
//
// $ php examples/03-benchmark.php unix:///tmp/server.sock
// $ php examples/91-benchmark-server.php unix:///tmp/server.sock
// $ nc -N -U /tmp/server.sock
// $ dd if=/dev/zero bs=1M count=1000 | nc -N -U /tmp/server.sock

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy