Skip to content

Commit 95bce45

Browse files
committed
Improve error reporting in server examples
1 parent 90d1e0b commit 95bce45

File tree

5 files changed

+47
-12
lines changed

5 files changed

+47
-12
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ $socket->on('error', function (Exception $e) {
238238
Note that this is not a fatal error event, i.e. the server keeps listening for
239239
new connections even after this event.
240240

241-
242241
#### getAddress()
243242

244243
The `getAddress(): ?string` method can be used to

examples/01-echo-server.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// You can also use systemd socket activation and listen on an inherited file descriptor:
2020
//
2121
// $ systemd-socket-activate -l 8000 php examples/01-echo-server.php php://fd/3
22+
// $ telnet localhost 8000
2223

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

@@ -31,8 +32,14 @@
3132
$socket->on('connection', function (React\Socket\ConnectionInterface $connection) {
3233
echo '[' . $connection->getRemoteAddress() . ' connected]' . PHP_EOL;
3334
$connection->pipe($connection);
35+
36+
$connection->on('close', function () use ($connection) {
37+
echo '[' . $connection->getRemoteAddress() . ' disconnected]' . PHP_EOL;
38+
});
3439
});
3540

36-
$socket->on('error', 'printf');
41+
$socket->on('error', function (Exception $e) {
42+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
43+
});
3744

3845
echo 'Listening on ' . $socket->getAddress() . PHP_EOL;

examples/02-chat-server.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// You can also use systemd socket activation and listen on an inherited file descriptor:
2020
//
2121
// $ systemd-socket-activate -l 8000 php examples/02-chat-server.php php://fd/3
22+
// $ telnet localhost 8000
2223

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

@@ -30,9 +31,11 @@
3031

3132
$socket = new React\Socket\LimitingServer($socket, null);
3233

33-
$socket->on('connection', function (React\Socket\ConnectionInterface $client) use ($socket) {
34+
$socket->on('connection', function (React\Socket\ConnectionInterface $connection) use ($socket) {
35+
echo '[' . $connection->getRemoteAddress() . ' connected]' . PHP_EOL;
36+
3437
// whenever a new message comes in
35-
$client->on('data', function ($data) use ($client, $socket) {
38+
$connection->on('data', function ($data) use ($connection, $socket) {
3639
// remove any non-word characters (just for the demo)
3740
$data = trim(preg_replace('/[^\w\d \.\,\-\!\?]/u', '', $data));
3841

@@ -42,13 +45,19 @@
4245
}
4346

4447
// prefix with client IP and broadcast to all connected clients
45-
$data = trim(parse_url($client->getRemoteAddress(), PHP_URL_HOST), '[]') . ': ' . $data . PHP_EOL;
48+
$data = trim(parse_url($connection->getRemoteAddress(), PHP_URL_HOST), '[]') . ': ' . $data . PHP_EOL;
4649
foreach ($socket->getConnections() as $connection) {
4750
$connection->write($data);
4851
}
4952
});
53+
54+
$connection->on('close', function () use ($connection) {
55+
echo '[' . $connection->getRemoteAddress() . ' disconnected]' . PHP_EOL;
56+
});
5057
});
5158

52-
$socket->on('error', 'printf');
59+
$socket->on('error', function (Exception $e) {
60+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
61+
});
5362

5463
echo 'Listening on ' . $socket->getAddress() . PHP_EOL;

examples/03-http-server.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
// $ php examples/03-http-server.php 127.0.0.1:8000
1616
// $ curl -v http://localhost:8000/
1717
// $ ab -n1000 -c10 http://localhost:8000/
18-
// $ docker run -it --rm --net=host jordi/ab ab -n1000 -c10 http://localhost:8000/
18+
// $ docker run -it --rm --net=host jordi/ab -n1000 -c10 http://localhost:8000/
1919
//
2020
// You can also run a secure HTTPS echo server like this:
2121
//
2222
// $ 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/
25-
// $ docker run -it --rm --net=host jordi/ab ab -n1000 -c10 https://localhost:8000/
25+
// $ docker run -it --rm --net=host jordi/ab -n1000 -c10 https://localhost:8000/
2626
//
2727
// You can also run a Unix domain socket (UDS) server like this:
2828
//
@@ -32,6 +32,9 @@
3232
// You can also use systemd socket activation and listen on an inherited file descriptor:
3333
//
3434
// $ systemd-socket-activate -l 8000 php examples/03-http-server.php php://fd/3
35+
// $ curl -v --insecure https://localhost:8000/
36+
// $ ab -n1000 -c10 https://localhost:8000/
37+
// $ docker run -it --rm --net=host jordi/ab -n1000 -c10 https://localhost:8000/
3538

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

@@ -42,12 +45,20 @@
4245
));
4346

4447
$socket->on('connection', function (React\Socket\ConnectionInterface $connection) {
48+
echo '[' . $connection->getRemoteAddress() . ' connected]' . PHP_EOL;
49+
4550
$connection->once('data', function () use ($connection) {
4651
$body = "<html><h1>Hello world!</h1></html>\r\n";
4752
$connection->end("HTTP/1.1 200 OK\r\nContent-Length: " . strlen($body) . "\r\nConnection: close\r\n\r\n" . $body);
4853
});
54+
55+
$connection->on('close', function () use ($connection) {
56+
echo '[' . $connection->getRemoteAddress() . ' disconnected]' . PHP_EOL;
57+
});
4958
});
5059

51-
$socket->on('error', 'printf');
60+
$socket->on('error', function (Exception $e) {
61+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
62+
});
5263

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

examples/91-benchmark-server.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
// $ 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
24+
//
25+
// You can also use systemd socket activation and listen on an inherited file descriptor:
26+
//
27+
// $ systemd-socket-activate -l 8000 php examples/91-benchmark-server.php php://fd/3
28+
// $ telnet localhost 8000
29+
// $ echo hello world | nc -N localhost 8000
30+
// $ dd if=/dev/zero bs=1M count=1000 | nc -N localhost 8000
2431

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

@@ -31,7 +38,7 @@
3138
));
3239

3340
$socket->on('connection', function (React\Socket\ConnectionInterface $connection) {
34-
echo '[connected]' . PHP_EOL;
41+
echo '[' . $connection->getRemoteAddress() . ' connected]' . PHP_EOL;
3542

3643
// count the number of bytes received from this connection
3744
$bytes = 0;
@@ -43,10 +50,12 @@
4350
$t = microtime(true);
4451
$connection->on('close', function () use ($connection, $t, &$bytes) {
4552
$t = microtime(true) - $t;
46-
echo '[disconnected after receiving ' . $bytes . ' bytes in ' . round($t, 3) . 's => ' . round($bytes / $t / 1024 / 1024, 1) . ' MiB/s]' . PHP_EOL;
53+
echo '[' . $connection->getRemoteAddress() . ' disconnected after receiving ' . $bytes . ' bytes in ' . round($t, 3) . 's => ' . round($bytes / $t / 1024 / 1024, 1) . ' MiB/s]' . PHP_EOL;
4754
});
4855
});
4956

50-
$socket->on('error', 'printf');
57+
$socket->on('error', function (Exception $e) {
58+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
59+
});
5160

5261
echo 'Listening on ' . $socket->getAddress() . PHP_EOL;

0 commit comments

Comments
 (0)