Skip to content

Commit

Permalink
fixed api service
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Apr 2, 2024
1 parent f32ebb5 commit 438530e
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,26 @@ public static function getStorage(): \Redis
/**
* @param Response $response
* @param TcpConnection $connection debug模式下传入null
* @param Request|null $request
* @return void
*/
public function send(Response $response, TcpConnection $connection): void
public function send(Response $response, TcpConnection $connection, ?Request $request = null): void
{
$response->withHeader('Content-Type', 'application/json');
$response->withHeader('Server', 'workbunny/webman-push-server');
$response->withHeader('Version', Server::$version);
$connection->send($response);
if ($request) {
$keepAlive = $request->header('connection');
if (
($keepAlive === null and $request->protocolVersion() === '1.1')
or $keepAlive === 'keep-alive'
or $keepAlive === 'Keep-Alive'
) {
$connection->send($response);
return;
}
}
$connection->close($response);
}

/** @inheritDoc */
Expand All @@ -76,7 +88,7 @@ public function onMessage(TcpConnection $connection, $data): void
$handler = $res[1] ?? null;
$params = $res[2] ?? [];
if(!$handler instanceof Closure) {
$this->send(\Workbunny\WebmanPushServer\response(404, 'Not Found.'), $connection);
$this->send(\Workbunny\WebmanPushServer\response(404, 'Not Found.'), $connection, $data);
return;
}
$response = call_user_func(array_reduce(
Expand All @@ -91,10 +103,10 @@ function (...$arguments) use ($handler) {
}
), Server::getServer(), $data, $params);
if(!$response instanceof Response){
$this->send(\Workbunny\WebmanPushServer\response(500, 'Server Error.'), $connection);
$this->send(\Workbunny\WebmanPushServer\response(500, 'Server Error.'), $connection, $data);
return;
}
$this->send($response, $connection);
$this->send($response, $connection, $data);
}

/** @inheritDoc */
Expand Down

0 comments on commit 438530e

Please sign in to comment.