array( 'local_cert' => isset($argv[2]) ? $argv[2] : (__DIR__ . '/localhost.pem') ) )); $socket->on('connection', function (React\Socket\ConnectionInterface $connection) { echo '[' . $connection->getRemoteAddress() . ' connected]' . PHP_EOL; // count the number of bytes received from this connection $bytes = 0; $connection->on('data', function ($chunk) use (&$bytes) { $bytes += strlen($chunk); }); // report average throughput once client disconnects $t = microtime(true); $connection->on('close', function () use ($connection, $t, &$bytes) { $t = microtime(true) - $t; 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', function (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; }); echo 'Listening on ' . $socket->getAddress() . PHP_EOL;