3
3
4
4
namespace SocketLog ;
5
5
6
+ use Dotenv \Dotenv ;
6
7
use Psr \Log \LoggerInterface ;
7
8
use Swoole \Http \Request ;
8
9
use Swoole \Http \Response ;
@@ -23,6 +24,16 @@ class Server
23
24
private array $ broadcastMap = [];
24
25
private array $ clientIdMap = [];
25
26
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
+
26
37
private array $ allowContentTypes = [
27
38
'application/json ' ,
28
39
'application/x-compress ' ,
@@ -31,13 +42,47 @@ class Server
31
42
public function __construct (
32
43
protected LoggerInterface $ logger ,
33
44
) {
45
+ $ this ->resolveConfig ();
34
46
$ this ->create ();
35
47
}
36
48
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
+
37
77
protected function create (): void
38
78
{
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
+ );
41
86
42
87
$ this ->logger ->info (\sprintf (
43
88
'listen: %s://%s ' ,
@@ -50,17 +95,13 @@ protected function create(): void
50
95
$ this ->listen ,
51
96
));
52
97
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
+ );
64
105
$ this ->logger ->info (\sprintf (
65
106
'listen: %s://%s ' ,
66
107
'ws ' ,
0 commit comments