diff --git a/HttpServer/Client/Http2.php b/HttpServer/Client/Http2.php index 50c5f818..7baeaf73 100644 --- a/HttpServer/Client/Http2.php +++ b/HttpServer/Client/Http2.php @@ -34,6 +34,17 @@ class Http2 extends Component } + /** + * @param int $timeout + * @return Http2 + */ + public function setTimeout(int $timeout): static + { + Context::setContext('http2timeout', $timeout); + return $this; + } + + /** * @param array $headers * @return Http2 @@ -153,7 +164,11 @@ class Http2 extends Component private function recv($client): mixed { /** @var Response $response */ - $response = $client->recv(); + if (!Context::hasContext('http2timeout')) { + $response = $client->recv(); + } else { + $response = $client->recv((int)Context::getContext('http2timeout')); + } if ($response === false || $response->statusCode > 200) { throw new Exception($client->errMsg, $client->errCode); } diff --git a/HttpServer/Events/OnPacket.php b/HttpServer/Events/OnPacket.php index 987710ee..f3697688 100644 --- a/HttpServer/Events/OnPacket.php +++ b/HttpServer/Events/OnPacket.php @@ -48,7 +48,7 @@ class OnPacket extends Callback $dispatch = $node->dispatch(); if (!is_string($dispatch)) $dispatch = Json::encode($dispatch); if (empty($dispatch)) { - return true; + $dispatch = Json::encode(['state' => 0, 'message' => 'ok']); } return $server->sendto($host, $port, $dispatch); } catch (\Throwable $exception) { diff --git a/HttpServer/Events/OnReceive.php b/HttpServer/Events/OnReceive.php index 5c74d36a..58f610c8 100644 --- a/HttpServer/Events/OnReceive.php +++ b/HttpServer/Events/OnReceive.php @@ -44,7 +44,7 @@ class OnReceive extends Callback $dispatch = $node->dispatch(); if (!is_string($dispatch)) $dispatch = Json::encode($dispatch); if (empty($dispatch)) { - return true; + $dispatch = Json::encode(['state' => 0, 'message' => 'ok']); } return $server->send($fd, $dispatch); } catch (\Throwable $exception) {