Skip to content

Commit 954a028

Browse files
committed
Fix issues like walkor/workerman#403
1 parent aed33af commit 954a028

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Manyou\WorkermanSymfonyRuntime;
6+
7+
use Chubbyphp\WorkermanRequestHandler\OnMessageInterface;
8+
use Chubbyphp\WorkermanRequestHandler\PsrRequestFactoryInterface;
9+
use Psr\Http\Server\RequestHandlerInterface;
10+
use Workerman\Connection\TcpConnection as WorkermanTcpConnection;
11+
use Workerman\Protocols\Http\Request as WorkermanRequest;
12+
use Workerman\Protocols\Http\Response as WorkermanResponse;
13+
14+
use function explode;
15+
use function in_array;
16+
use function strtolower;
17+
18+
final class OnMessage implements OnMessageInterface
19+
{
20+
public function __construct(
21+
private PsrRequestFactoryInterface $psrRequestFactory,
22+
private RequestHandlerInterface $requestHander,
23+
) {
24+
}
25+
26+
public function __invoke(WorkermanTcpConnection $workermanTcpConnection, WorkermanRequest $workermanRequest): void
27+
{
28+
$response = $this->requestHander->handle(
29+
$this->psrRequestFactory->create($workermanTcpConnection, $workermanRequest),
30+
);
31+
32+
$workermanTcpConnection->send(
33+
(new WorkermanResponse())
34+
->withStatus($response->getStatusCode(), $response->getReasonPhrase())
35+
->withHeaders($response->getHeaders())
36+
->withBody((string) $response->getBody()),
37+
);
38+
39+
$keepAlive = in_array('keep-alive', explode(',', strtolower($workermanRequest->header('connection', 'close'))), true);
40+
if (! $keepAlive) {
41+
$workermanTcpConnection->close();
42+
}
43+
}
44+
}

packages/workerman-symfony-runtime/Resources/config/services.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
66

7-
use Chubbyphp\WorkermanRequestHandler\OnMessage;
87
use Chubbyphp\WorkermanRequestHandler\OnMessageInterface;
98
use Chubbyphp\WorkermanRequestHandler\PsrRequestFactory;
109
use Chubbyphp\WorkermanRequestHandler\PsrRequestFactoryInterface;
11-
use Chubbyphp\WorkermanRequestHandler\WorkermanResponseEmitter;
12-
use Chubbyphp\WorkermanRequestHandler\WorkermanResponseEmitterInterface;
1310
use Manyou\WorkermanSymfonyRuntime\HeaderNameMapper;
11+
use Manyou\WorkermanSymfonyRuntime\OnMessage;
1412
use Manyou\WorkermanSymfonyRuntime\SymfonyRequestHandler;
1513
use Psr\Http\Server\RequestHandlerInterface;
1614
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
@@ -50,8 +48,6 @@
5048
// Workerman Request to PSR Request
5149
$services->set(PsrRequestFactoryInterface::class, PsrRequestFactory::class);
5250

53-
$services->set(WorkermanResponseEmitterInterface::class, WorkermanResponseEmitter::class);
54-
5551
$services->set(SymfonyRequestHandler::class)
5652
->arg(HttpKernelInterface::class, service('kernel'));
5753

packages/workerman-symfony-runtime/Runtime.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ public function __construct(array $options = [])
2323
$this->socket = $options['socket']
2424
?? $_SERVER['APP_RUNTIME_SOCKET']
2525
?? $_ENV['APP_RUNTIME_SOCKET']
26-
?? 'http://0.0.0.0:' . ($_SERVER['PORT'] ?? $_ENV['PORT'] ?? 8283);
26+
?? 'http://0.0.0.0:' . ($_SERVER['PORT'] ?? $_ENV['PORT'] ?? 3000);
2727

2828
$this->workers = (int) ($options['workers']
2929
?? $_SERVER['APP_RUNTIME_WORKERS']
3030
?? $_ENV['APP_RUNTIME_WORKERS']
31-
?? 1);
31+
?? 16);
3232

3333
$hash = md5(__FILE__);
3434

0 commit comments

Comments
 (0)