Skip to content

Commit b43838b

Browse files
authored
Merge pull request #75 from clue-labs/examples
Add examples
2 parents 223e0a3 + 16ef9c8 commit b43838b

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ $tcpConnector->create('127.0.0.1', 80)->then(function (React\Stream\Stream $stre
4545
$loop->run();
4646
```
4747

48+
See also the [first example](examples).
49+
4850
Pending connection attempts can be cancelled by cancelling its pending promise like so:
4951

5052
```php
@@ -95,6 +97,8 @@ $dnsConnector->create('www.google.com', 80)->then(function (React\Stream\Stream
9597
$loop->run();
9698
```
9799

100+
See also the [first example](examples).
101+
98102
Pending connection attempts can be cancelled by cancelling its pending promise like so:
99103

100104
```php
@@ -135,6 +139,8 @@ $secureConnector->create('www.google.com', 443)->then(function (React\Stream\Str
135139
$loop->run();
136140
```
137141

142+
See also the [second example](examples).
143+
138144
Pending connection attempts can be cancelled by cancelling its pending promise like so:
139145

140146
```php
@@ -179,6 +185,8 @@ $timeoutConnector->create('google.com', 80)->then(function (React\Stream\Stream
179185
});
180186
```
181187

188+
See also any of the [examples](examples).
189+
182190
Pending connection attempts can be cancelled by cancelling its pending promise like so:
183191

184192
```php

examples/01-http.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
use React\SocketClient\TcpConnector;
5+
use React\SocketClient\DnsConnector;
6+
use React\Stream\Stream;
7+
use React\SocketClient\TimeoutConnector;
8+
9+
require __DIR__ . '/../vendor/autoload.php';
10+
11+
$loop = Factory::create();
12+
13+
$factory = new \React\Dns\Resolver\Factory();
14+
$resolver = $factory->create('8.8.8.8', $loop);
15+
16+
$tcp = new TcpConnector($loop);
17+
$dns = new DnsConnector($tcp, $resolver);
18+
19+
// time out connection attempt in 3.0s
20+
$dns = new TimeoutConnector($dns, 3.0, $loop);
21+
22+
$dns->create('www.google.com', 80)->then(function (Stream $stream) {
23+
$stream->on('data', function ($data) {
24+
echo $data;
25+
});
26+
$stream->on('close', function () {
27+
echo '[CLOSED]' . PHP_EOL;
28+
});
29+
30+
$stream->write("GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n");
31+
}, 'printf');
32+
33+
$loop->run();

examples/02-https.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
use React\SocketClient\TcpConnector;
5+
use React\SocketClient\DnsConnector;
6+
use React\SocketClient\SecureConnector;
7+
use React\Stream\Stream;
8+
use React\SocketClient\TimeoutConnector;
9+
10+
require __DIR__ . '/../vendor/autoload.php';
11+
12+
$loop = Factory::create();
13+
14+
$factory = new \React\Dns\Resolver\Factory();
15+
$resolver = $factory->create('8.8.8.8', $loop);
16+
17+
$tcp = new TcpConnector($loop);
18+
$dns = new DnsConnector($tcp, $resolver);
19+
$tls = new SecureConnector($dns, $loop);
20+
21+
// time out connection attempt in 3.0s
22+
$tls = new TimeoutConnector($tls, 3.0, $loop);
23+
24+
$tls->create('www.google.com', 443)->then(function (Stream $stream) {
25+
$stream->on('data', function ($data) {
26+
echo $data;
27+
});
28+
$stream->on('close', function () {
29+
echo '[CLOSED]' . PHP_EOL;
30+
});
31+
32+
$stream->write("GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n");
33+
}, 'printf');
34+
35+
$loop->run();

examples/03-netcat.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
use React\SocketClient\TcpConnector;
5+
use React\SocketClient\DnsConnector;
6+
use React\Stream\Stream;
7+
use React\SocketClient\TimeoutConnector;
8+
9+
require __DIR__ . '/../vendor/autoload.php';
10+
11+
if (!isset($argv[2])) {
12+
fwrite(STDERR, 'Usage error: required arguments <host> <port>' . PHP_EOL);
13+
exit(1);
14+
}
15+
16+
$loop = Factory::create();
17+
18+
$factory = new \React\Dns\Resolver\Factory();
19+
$resolver = $factory->create('8.8.8.8', $loop);
20+
21+
$tcp = new TcpConnector($loop);
22+
$dns = new DnsConnector($tcp, $resolver);
23+
24+
// time out connection attempt in 3.0s
25+
$dns = new TimeoutConnector($dns, 3.0, $loop);
26+
27+
$stdin = new Stream(STDIN, $loop);
28+
$stdin->pause();
29+
$stdout = new Stream(STDOUT, $loop);
30+
$stdout->pause();
31+
$stderr = new Stream(STDERR, $loop);
32+
$stderr->pause();
33+
34+
$stderr->write('Connecting' . PHP_EOL);
35+
36+
$dns->create($argv[1], $argv[2])->then(function (Stream $stream) use ($stdin, $stdout, $stderr) {
37+
// pipe everything from STDIN into connection
38+
$stdin->resume();
39+
$stdin->pipe($stream);
40+
41+
// pipe everything from connection to STDOUT
42+
$stream->pipe($stdout);
43+
44+
// report errors to STDERR
45+
$stream->on('error', function ($error) use ($stderr) {
46+
$stderr->write('Stream ERROR: ' . $error . PHP_EOL);
47+
});
48+
49+
// report closing and stop reading from input
50+
$stream->on('close', function () use ($stderr, $stdin) {
51+
$stderr->write('[CLOSED]' . PHP_EOL);
52+
$stdin->close();
53+
});
54+
55+
$stderr->write('Connected' . PHP_EOL);
56+
}, function ($error) use ($stderr) {
57+
$stderr->write('Connection ERROR: ' . $error . PHP_EOL);
58+
});
59+
60+
$loop->run();

0 commit comments

Comments
 (0)