Skip to content

Commit

Permalink
Fix issues like walkor/workerman#403
Browse files Browse the repository at this point in the history
  • Loading branch information
bohanyang committed May 19, 2024
1 parent aed33af commit 954a028
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
44 changes: 44 additions & 0 deletions packages/workerman-symfony-runtime/OnMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Manyou\WorkermanSymfonyRuntime;

use Chubbyphp\WorkermanRequestHandler\OnMessageInterface;
use Chubbyphp\WorkermanRequestHandler\PsrRequestFactoryInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Workerman\Connection\TcpConnection as WorkermanTcpConnection;
use Workerman\Protocols\Http\Request as WorkermanRequest;
use Workerman\Protocols\Http\Response as WorkermanResponse;

use function explode;
use function in_array;
use function strtolower;

final class OnMessage implements OnMessageInterface
{
public function __construct(
private PsrRequestFactoryInterface $psrRequestFactory,
private RequestHandlerInterface $requestHander,
) {
}

public function __invoke(WorkermanTcpConnection $workermanTcpConnection, WorkermanRequest $workermanRequest): void
{
$response = $this->requestHander->handle(
$this->psrRequestFactory->create($workermanTcpConnection, $workermanRequest),
);

$workermanTcpConnection->send(
(new WorkermanResponse())
->withStatus($response->getStatusCode(), $response->getReasonPhrase())
->withHeaders($response->getHeaders())
->withBody((string) $response->getBody()),
);

$keepAlive = in_array('keep-alive', explode(',', strtolower($workermanRequest->header('connection', 'close'))), true);
if (! $keepAlive) {
$workermanTcpConnection->close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Chubbyphp\WorkermanRequestHandler\OnMessage;
use Chubbyphp\WorkermanRequestHandler\OnMessageInterface;
use Chubbyphp\WorkermanRequestHandler\PsrRequestFactory;
use Chubbyphp\WorkermanRequestHandler\PsrRequestFactoryInterface;
use Chubbyphp\WorkermanRequestHandler\WorkermanResponseEmitter;
use Chubbyphp\WorkermanRequestHandler\WorkermanResponseEmitterInterface;
use Manyou\WorkermanSymfonyRuntime\HeaderNameMapper;
use Manyou\WorkermanSymfonyRuntime\OnMessage;
use Manyou\WorkermanSymfonyRuntime\SymfonyRequestHandler;
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
Expand Down Expand Up @@ -50,8 +48,6 @@
// Workerman Request to PSR Request
$services->set(PsrRequestFactoryInterface::class, PsrRequestFactory::class);

$services->set(WorkermanResponseEmitterInterface::class, WorkermanResponseEmitter::class);

$services->set(SymfonyRequestHandler::class)
->arg(HttpKernelInterface::class, service('kernel'));

Expand Down
4 changes: 2 additions & 2 deletions packages/workerman-symfony-runtime/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public function __construct(array $options = [])
$this->socket = $options['socket']
?? $_SERVER['APP_RUNTIME_SOCKET']
?? $_ENV['APP_RUNTIME_SOCKET']
?? 'http://0.0.0.0:' . ($_SERVER['PORT'] ?? $_ENV['PORT'] ?? 8283);
?? 'http://0.0.0.0:' . ($_SERVER['PORT'] ?? $_ENV['PORT'] ?? 3000);

$this->workers = (int) ($options['workers']
?? $_SERVER['APP_RUNTIME_WORKERS']
?? $_ENV['APP_RUNTIME_WORKERS']
?? 1);
?? 16);

$hash = md5(__FILE__);

Expand Down

0 comments on commit 954a028

Please sign in to comment.