8000 Improve error messages for failed connections to include errno by clue · Pull Request #271 · reactphp/socket · GitHub
[go: up one dir, main page]

Skip to content

Improve error messages for failed connections to include errno #271

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 7 commits into from
Sep 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Improve error reporting in server examples
  • Loading branch information
clue committed Sep 6, 2021
commit 95bce45647dbd9428489e4ead8a0f41b4ddb68f2
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ $socket->on('error', function (Exception $e) {
Note that this is not a fatal error event, i.e. the server keeps listening for
new connections even after this event.


#### getAddress()

The `getAddress(): ?string` method can be used to
Expand Down
9 changes: 8 additions & 1 deletion examples/01-echo-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// You can also use systemd socket activation and listen on an inherited file descriptor:
//
// $ systemd-socket-activate -l 8000 php examples/01-echo-server.php php://fd/3
// $ telnet localhost 8000

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

Expand All @@ -31,8 +32,14 @@
$socket->on('connection', function (React\Socket\ConnectionInterface $connection) {
echo '[' . $connection->getRemoteAddress() . ' connected]' . PHP_EOL;
$connection->pipe($connection);

$connection->on('close', function () use ($connection) {
echo '[' . $connection->getRemoteAddress() . ' disconnected]' . PHP_EOL;
});
});

$socket->on('error', 'printf');
$socket->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

echo 'Listening on ' . $socket->getAddress() . PHP_EOL;
17 changes: 13 additions & 4 deletions examples/02-chat-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// You can also use systemd socket activation and listen on an inherited file descriptor:
//
// $ systemd-socket-activate -l 8000 php examples/02-chat-server.php php://fd/3
// $ telnet localhost 8000

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

Expand All @@ -30,9 +31,11 @@

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

$socket->on('connection', function (React\Socket\ConnectionInterface $client) use ($socket) {
$socket->on('connection', function (React\Socket\ConnectionInterface $connection) use ($socket) {
echo '[' . $connection->getRemoteAddress() . ' connected]' . PHP_EOL;

// whenever a new message comes in
$client->on('data', function ($data) use ($client, $socket) {
$connection->on('data', function ($data) use ($connection, $socket) {
// remove any non-word characters (just for the demo)
$data = trim(preg_replace('/[^\w\d \.\,\-\!\?]/u', '', $data));

Expand All @@ -42,13 +45,19 @@
}

// prefix with client IP and broadcast to all connected clients
$data = trim(parse_url($client->getRemoteAddress(), PHP_URL_HOST), '[]') . ': ' . $data . PHP_EOL;
$data = trim(parse_url($connection->getRemoteAddress(), PHP_URL_HOST), '[]') . ': ' . $data . PHP_EOL;
foreach ($socket->getConnections() as $connection) {
$connection->write($data);
}
});

$connection->on('close', function () use ($connection) {
echo '[' . $connection->getRemoteAddress() . ' disconnected]' . PHP_EOL;
});
});

$socket->on('error', 'printf');
$socket->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

echo 'Listening on ' . $socket->getAddress() . PHP_EOL;
17 changes: 14 additions & 3 deletions examples/03-http-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
// $ php examples/03-http-server.php 127.0.0.1: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/
// $ docker run -it --rm --net=host jordi/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/
// $ docker run -it --rm --net=host jordi/ab -n1000 -c10 https://localhost:8000/
//
// You can also run a Unix domain socket (UDS) server like this:
//
Expand All @@ -32,6 +32,9 @@
// You can also use systemd socket activation and listen on an inherited file descriptor:
//
// $ systemd-socket-activate -l 8000 php examples/03-http-server.php php://fd/3
// $ curl -v --insecure https://localhost:8000/
// $ ab -n1000 -c10 https://localhost:8000/
// $ docker run -it --rm --net=host jordi/ab -n1000 -c10 https://localhost:8000/

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

Expand All @@ -42,12 +45,20 @@
));

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

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

$connection->on('close', function () use ($connection) {
echo '[' . $connection->getRemoteAddress() . ' disconnected]' . PHP_EOL;
});
});

$socket->on('error', 'printf');
$socket->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

echo 'Listening on ' . strtr($socket->getAddress(), array('tcp:' => 'http:', 'tls:' => 'https:')) . PHP_EOL;
15 changes: 12 additions & 3 deletions examples/91-benchmark-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
// $ 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
//
// You can also use systemd socket activation and listen on an inherited file descriptor:
//
// $ systemd-socket-activate -l 8000 php examples/91-benchmark-server.php php://fd/3
// $ telnet localhost 8000
// $ echo hello world | nc -N localhost 8000
// $ dd if=/dev/zero bs=1M count=1000 | nc -N localhost 8000

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

Expand All @@ -31,7 +38,7 @@
));

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

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

$socket->on('error', 'printf');
$socket->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

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