Skip to content

Commit 75afcc7

Browse files
committed
update: 支持配置监听端口,支持ipv6,支持控制进程数
1 parent ce50db8 commit 75afcc7

File tree

6 files changed

+342
-18
lines changed

6 files changed

+342
-18
lines changed

composer.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@
1313
"psr-4": {
1414
"SocketLog\\": "src/"
1515
},
16-
"files": []
16+
"files": [
17+
"src/func.php"
18+
]
1719
},
1820
"require": {
1921
"php": "^8.0",
2022
"ext-ctype": "*",
21-
"ext-zlib": "*",
2223
"ext-mbstring": "*",
24+
"ext-zlib": "*",
2325
"psr/log": "^1.1",
2426
"symfony/console": "^6.0",
27+
"vlucas/phpdotenv": "^5.5",
2528
"zxin/utils": "^2.4"
2629
},
2730
"require-dev": {

composer.lock

+224-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.php

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace SocketLog;
55

6+
use Dotenv\Dotenv;
67
use Phar;
78
use Symfony\Component\Console\Application;
89
use function define;
@@ -18,6 +19,13 @@
1819

1920
require __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
2021

22+
if (\is_file(__DIR__ . DIRECTORY_SEPARATOR . '.env')) {
23+
$dotenv = Dotenv::createMutable(__DIR__);
24+
$dotenv->load();
25+
26+
Server::verifyEnv($dotenv);
27+
}
28+
2129
$app = new Application('socket-log-server', VERSION_TITLE);
2230
$command = new ServerCommand();
2331
$app->add($command);

src/Server.php

+54-13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace SocketLog;
55

6+
use Dotenv\Dotenv;
67
use Psr\Log\LoggerInterface;
78
use Swoole\Http\Request;
89
use Swoole\Http\Response;
@@ -23,6 +24,16 @@ class Server
2324
private array $broadcastMap = [];
2425
private array $clientIdMap = [];
2526

27+
private array $config = [
28+
'worker_num' => 1,
29+
'daemonize' => false,
30+
'heartbeat_check_interval' => 60,
31+
'heartbeat_idle_time' => 300,
32+
'pid_file' => RUNTIME_DIR . '/server.pid',
33+
'http_compression' => true,
34+
'websocket_compression' => true,
35+
];
36+
2637
private array $allowContentTypes = [
2738
'application/json',
2839
'application/x-compress',
@@ -31,13 +42,47 @@ class Server
3142
public function __construct(
3243
protected LoggerInterface $logger,
3344
) {
45+
$this->resolveConfig();
3446
$this->create();
3547
}
3648

49+
public static function verifyEnv(Dotenv $dotenv): void
50+
{
51+
$dotenv
52+
->ifPresent('SL_SERVER_LISTEN')
53+
->assert(
54+
fn ($val) => test_address_and_port($val),
55+
\sprintf('(%s) address is invalid', $_ENV['SL_SERVER_LISTEN'] ?? '')
56+
);
57+
58+
$dotenv
59+
->ifPresent('SL_SERVER_BC_LISTEN')
60+
->assert(function (string $val): bool {
61+
if ('false' === $val) {
62+
return true;
63+
}
64+
return test_address_and_port($val);
65+
}, \sprintf('(%s) address is invalid', $_ENV['SL_SERVER_BC_LISTEN'] ?? ''));
66+
67+
$dotenv->ifPresent('SL_WORKER_NUM')
68+
->isInteger();
69+
}
70+
71+
protected function resolveConfig(): void
72+
{
73+
$this->listen = $_ENV['SL_SERVER_LISTEN'] ?? $this->listen;
74+
$this->listenWS = $_ENV['SL_SERVER_BC_LISTEN'] ?? $this->listenWS;
75+
}
76+
3777
protected function create(): void
3878
{
39-
$listen = \explode(':', $this->listen);
40-
$server = new \Swoole\WebSocket\Server($listen[0] ?: '127.0.0.1', (int) ($listen[1] ?? 1116), SWOOLE_BASE);
79+
$listen = parse_str_ip_and_port($this->listen);
80+
$server = new \Swoole\WebSocket\Server(
81+
$listen[0] ?: '127.0.0.1',
82+
(int) ($listen[1] ?? 1116),
83+
SWOOLE_BASE,
84+
SWOOLE_SOCK_TCP | SWOOLE_SOCK_TCP6,
85+
);
4186

4287
$this->logger->info(\sprintf(
4388
'listen: %s://%s',
@@ -50,17 +95,13 @@ protected function create(): void
5095
$this->listen,
5196
));
5297

53-
$server->set([
54-
'worker_num' => 1,
55-
'daemonize' => false,
56-
'heartbeat_check_interval' => 60,
57-
'heartbeat_idle_time' => 300,
58-
'pid_file' => RUNTIME_DIR . '/server.pid',
59-
'http_compression' => true,
60-
'websocket_compression' => true,
61-
]);
62-
$listen = \explode(':', $this->listenWS);
63-
$server->addlistener($listen[0] ?: '127.0.0.1', (int) ($listen[1] ?? 1229), $server->mode);
98+
$server->set($this->config);
99+
$listen = parse_str_ip_and_port($this->listenWS);
100+
$server->addlistener(
101+
$listen[0] ?: '127.0.0.1',
102+
(int) ($listen[1] ?? 1229),
103+
$server->mode,
104+
);
64105
$this->logger->info(\sprintf(
65106
'listen: %s://%s',
66107
'ws',

0 commit comments

Comments
 (0)