diff --git a/README.md b/README.md index f5e5edf..4215709 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,8 @@ $tcpConnector->create('127.0.0.1', 80)->then(function (React\Stream\Stream $stre $loop->run(); ``` +See also the [first example](examples). + Pending connection attempts can be cancelled by cancelling its pending promise like so: ```php @@ -95,6 +97,8 @@ $dnsConnector->create('www.google.com', 80)->then(function (React\Stream\Stream $loop->run(); ``` +See also the [first example](examples). + Pending connection attempts can be cancelled by cancelling its pending promise like so: ```php @@ -135,6 +139,8 @@ $secureConnector->create('www.google.com', 443)->then(function (React\Stream\Str $loop->run(); ``` +See also the [second example](examples). + Pending connection attempts can be cancelled by cancelling its pending promise like so: ```php @@ -171,6 +177,8 @@ $timeoutConnector->create('google.com', 80)->then(function (React\Stream\Stream }); ``` +See also any of the [examples](examples). + Pending connection attempts can be cancelled by cancelling its pending promise like so: ```php diff --git a/examples/01-http.php b/examples/01-http.php new file mode 100644 index 0000000..6a2f931 --- /dev/null +++ b/examples/01-http.php @@ -0,0 +1,33 @@ +create('8.8.8.8', $loop); + +$tcp = new TcpConnector($loop); +$dns = new DnsConnector($tcp, $resolver); + +// time out connection attempt in 3.0s +$dns = new TimeoutConnector($dns, 3.0, $loop); + +$dns->create('www.google.com', 80)->then(function (Stream $stream) { + $stream->on('data', function ($data) { + echo $data; + }); + $stream->on('close', function () { + echo '[CLOSED]' . PHP_EOL; + }); + + $stream->write("GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n"); +}, 'printf'); + +$loop->run(); diff --git a/examples/02-https.php b/examples/02-https.php new file mode 100644 index 0000000..c70ddcd --- /dev/null +++ b/examples/02-https.php @@ -0,0 +1,35 @@ +create('8.8.8.8', $loop); + +$tcp = new TcpConnector($loop); +$dns = new DnsConnector($tcp, $resolver); +$tls = new SecureConnector($dns, $loop); + +// time out connection attempt in 3.0s +$tls = new TimeoutConnector($tls, 3.0, $loop); + +$tls->create('www.google.com', 443)->then(function (Stream $stream) { + $stream->on('data', function ($data) { + echo $data; + }); + $stream->on('close', function () { + echo '[CLOSED]' . PHP_EOL; + }); + + $stream->write("GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n"); +}, 'printf'); + +$loop->run(); diff --git a/examples/03-netcat.php b/examples/03-netcat.php new file mode 100644 index 0000000..8ef34ad --- /dev/null +++ b/examples/03-netcat.php @@ -0,0 +1,60 @@ + ' . PHP_EOL); + exit(1); +} + +$loop = Factory::create(); + +$factory = new \React\Dns\Resolver\Factory(); +$resolver = $factory->create('8.8.8.8', $loop); + +$tcp = new TcpConnector($loop); +$dns = new DnsConnector($tcp, $resolver); + +// time out connection attempt in 3.0s +$dns = new TimeoutConnector($dns, 3.0, $loop); + +$stdin = new Stream(STDIN, $loop); +$stdin->pause(); +$stdout = new Stream(STDOUT, $loop); +$stdout->pause(); +$stderr = new Stream(STDERR, $loop); +$stderr->pause(); + +$stderr->write('Connecting' . PHP_EOL); + +$dns->create($argv[1], $argv[2])->then(function (Stream $stream) use ($stdin, $stdout, $stderr) { + // pipe everything from STDIN into connection + $stdin->resume(); + $stdin->pipe($stream); + + // pipe everything from connection to STDOUT + $stream->pipe($stdout); + + // report errors to STDERR + $stream->on('error', function ($error) use ($stderr) { + $stderr->write('Stream ERROR: ' . $error . PHP_EOL); + }); + + // report closing and stop reading from input + $stream->on('close', function () use ($stderr, $stdin) { + $stderr->write('[CLOSED]' . PHP_EOL); + $stdin->close(); + }); + + $stderr->write('Connected' . PHP_EOL); +}, function ($error) use ($stderr) { + $stderr->write('Connection ERROR: ' . $error . PHP_EOL); +}); + +$loop->run();