This commit is contained in:
2021-04-01 11:43:24 +08:00
parent 5fc9d5c61d
commit 2ac138182b
3 changed files with 18 additions and 3 deletions
+16 -1
View File
@@ -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 * @param array $headers
* @return Http2 * @return Http2
@@ -153,7 +164,11 @@ class Http2 extends Component
private function recv($client): mixed private function recv($client): mixed
{ {
/** @var Response $response */ /** @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) { if ($response === false || $response->statusCode > 200) {
throw new Exception($client->errMsg, $client->errCode); throw new Exception($client->errMsg, $client->errCode);
} }
+1 -1
View File
@@ -48,7 +48,7 @@ class OnPacket extends Callback
$dispatch = $node->dispatch(); $dispatch = $node->dispatch();
if (!is_string($dispatch)) $dispatch = Json::encode($dispatch); if (!is_string($dispatch)) $dispatch = Json::encode($dispatch);
if (empty($dispatch)) { if (empty($dispatch)) {
return true; $dispatch = Json::encode(['state' => 0, 'message' => 'ok']);
} }
return $server->sendto($host, $port, $dispatch); return $server->sendto($host, $port, $dispatch);
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
+1 -1
View File
@@ -44,7 +44,7 @@ class OnReceive extends Callback
$dispatch = $node->dispatch(); $dispatch = $node->dispatch();
if (!is_string($dispatch)) $dispatch = Json::encode($dispatch); if (!is_string($dispatch)) $dispatch = Json::encode($dispatch);
if (empty($dispatch)) { if (empty($dispatch)) {
return true; $dispatch = Json::encode(['state' => 0, 'message' => 'ok']);
} }
return $server->send($fd, $dispatch); return $server->send($fd, $dispatch);
} catch (\Throwable $exception) { } catch (\Throwable $exception) {