Skip to content
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions examples/01-http.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use React\EventLoop\Factory;
use React\SocketClient\TcpConnector;
use React\SocketClient\DnsConnector;
use React\Stream\Stream;
use React\SocketClient\TimeoutConnector;

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

$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);

$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();
35 changes: 35 additions & 0 deletions examples/02-https.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use React\EventLoop\Factory;
use React\SocketClient\TcpConnector;
use React\SocketClient\DnsConnector;
use React\SocketClient\SecureConnector;
use React\Stream\Stream;
use React\SocketClient\TimeoutConnector;

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

$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);
$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();
60 changes: 60 additions & 0 deletions examples/03-netcat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

use React\EventLoop\Factory;
use React\SocketClient\TcpConnector;
use React\SocketClient\DnsConnector;
use React\Stream\Stream;
use React\SocketClient\TimeoutConnector;

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

if (!isset($argv[2])) {
fwrite(STDERR, 'Usage error: required arguments <host> <port>' . 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();