public function onRead($client, $nread, $buffer) { //echo $buffer; //echo "--Error: " . uv_err_name(uv_last_error(uv_default_loop())) . PHP_EOL; if ($nread < 0) { //echo "[NREAD={$nread}]\n"; uv_shutdown($client, array($this, "onShutdown")); } else { if ($nread == 0) { // nothing to do. //echo "[NREAD=0]\n"; } else { $result = array(); if (uv_http_parser_execute($this->parsers[(int) $client], $buffer, $result)) { $response = new HttpResponse($this, $client); $closure = $this->closure; $closure($result, $response, $client); } else { // nothing to do. (waiting next buffer) } } } }
$parsers[(int) $client] = uv_http_parser_init(); uv_read_start($client, function ($client, $nread, $buffer) use(&$parsers, &$clients) { if ($nread < 0) { uv_shutdown($client, function ($client) use(&$parsers, &$clients) { uv_close($client, function ($client) use(&$parsers, &$clients) { unset($parsers[(int) $client]); unset($clients[(int) $client]); }); }); return; } else { if ($nread == 0) { if (uv_last_error() == UV::EOF) { uv_shutdown($client, function ($client) use(&$parsers, &$clients) { uv_close($client, function ($client) use(&$parsers, &$clients) { unset($parsers[(int) $client]); unset($clients[(int) $client]); }); }); return; } } else { $result = array(); if (uv_http_parser_execute($parsers[(int) $client], $buffer, $result)) { $response = "HTTP/1.1 200 OK\r\n\r\nHello World"; uv_write($client, $response, function ($client) use(&$parsers, &$clients) { uv_close($client, function ($client) use(&$parsers, &$clients) { unset($parsers[(int) $client]); unset($clients[(int) $client]); }); }); }