55
66use swoole_websocket_and_tcp_and_udp \common \Logger ;
77use swoole_websocket_and_tcp_and_udp \common \ProcessTrait ;
8+ use swoole_websocket_and_tcp_and_udp \common \Swoole ;
89use swoole_websocket_and_tcp_and_udp \handler \HttpHandler ;
10+ use swoole_websocket_and_tcp_and_udp \handler \ServerHandler ;
911use swoole_websocket_and_tcp_and_udp \handler \WebsocketHandler ;
1012
13+ /**
14+ * @description 此文件多处借鉴 @laravel-s
15+ * @link https://github.com/hhxsv5/laravel-s/blob/master/src/Swoole/Server.php#L65
16+ */
1117class Server
1218{
13- use ProcessTrait, Logger;
19+ use ProcessTrait, Logger, Swoole ;
1420
1521 protected $ port ;
1622
@@ -27,34 +33,71 @@ class Server
2733
2834 protected $ enableTask = false ;
2935
36+ protected $ otherConfig = [];
37+
3038 public function __construct ($ config )
3139 {
40+ $ this ->checkSwoole ('1.10.4 ' );
41+
42+ $ this ->primaryConfig = [
43+ 'host ' => '0.0.0.0 ' ,
44+ 'port ' => '8999 ' ,
45+ 'type ' => SWOOLE_SOCK_TCP ,
46+ 'setting ' => [
47+ 'daemonize ' => false ,
48+ 'open_http_protocol ' => true ,
49+ ],
50+ 'handler ' => \swoole_websocket_and_tcp_and_udp_test \http::class,
51+ ];
52+ $ serverClass = \swoole_http_server::class;
53+
3254 $ this ->config = $ config ;
3355 ini_set ('date.timezone ' , $ this ->config ['timezone ' ]);
3456
3557 if (isset ($ this ->config ['websocket ' ])) {
36- $ this ->enableWebsocket = true ;
37- $ this ->primaryConfig = $ this ->config ['websocket ' ];
38- $ this ->primaryConfig ['setting ' ]['open_websocket_protocol ' ] = true ;
39- if (!isset ($ this ->primaryConfig ['setting ' ]['open_http_protocol ' ])) {
40- $ this ->primaryConfig ['setting ' ]['open_http_protocol ' ] = true ;
41- }
42- if (!isset ($ this ->primaryConfig ['setting ' ]['daemonize ' ])) {
43- $ this ->primaryConfig ['setting ' ]['daemonize ' ] = false ;
58+ if ($ this ->config ['websocket ' ]['enable ' ]) {
59+ $ this ->enableWebsocket = true ;
60+ $ this ->primaryConfig = $ this ->config ['websocket ' ];
61+ $ this ->primaryConfig ['setting ' ]['open_websocket_protocol ' ]
62+ = true ;
63+ if (!isset ($ this ->primaryConfig ['setting ' ]['open_http_protocol ' ])) {
64+ $ this ->primaryConfig ['setting ' ]['open_http_protocol ' ]
65+ = true ;
66+ }
67+ if (!isset ($ this ->primaryConfig ['setting ' ]['daemonize ' ])) {
68+ $ this ->primaryConfig ['setting ' ]['daemonize ' ] = false ;
69+ }
70+ $ serverClass = \swoole_websocket_server::class;
71+ unset($ this ->config ['websocket ' ]);
72+
73+ goto start;
4474 }
75+ }
4576
46- $ serverClass = \swoole_websocket_server::class;
47- } else {
48- if (isset ($ this ->config ['http ' ])) {
77+ if (isset ($ this ->config ['http ' ])) {
78+ if ($ this ->config ['http ' ]['enable ' ]) {
4979 $ this ->primaryConfig = $ this ->config ['http ' ];
5080 $ this ->primaryConfig ['setting ' ]['open_http_protocol ' ] = true ;
51- if (!isset ($ this ->primaryConfig ['setting ' ]['daemonize ' ])) {
81+ if (!isset ($ this ->primaryConfig ['setting ' ]['daemonize ' ])) {
5282 $ this ->primaryConfig ['setting ' ]['daemonize ' ] = false ;
5383 }
54-
5584 $ serverClass = \swoole_http_server::class;
85+ unset($ this ->config ['http ' ]);
86+
87+ goto start;
88+ }
89+ }
90+
91+ start:
92+
93+ foreach ($ this ->config as $ name => $ item ) {
94+ if (!is_array ($ item )) {
95+ continue ;
5696 }
97+
98+ $ this ->otherConfig [$ name ] = $ item ;
5799 }
100+ // var_dump($this->otherConfig);exit(1);
58101
59102 $ host = $ this ->primaryConfig ['host ' ];
60103 $ port = $ this ->primaryConfig ['port ' ];
@@ -65,7 +108,8 @@ public function __construct($config)
65108 $ this ->enableTask = true ;
66109 }
67110
68- Logger::info ("开始监听端口 {$ host }: {$ port }" );
111+ $ serverName = $ this ->enableWebsocket ?'websocket ' :'http ' ;
112+ Logger::info ("开始监听 [ {$ serverName }] {$ host }: {$ port }" );
69113 $ this ->server = new $ serverClass ($ host , $ port , SWOOLE_PROCESS , $ type );
70114 $ this ->server ->set ($ setting );
71115
@@ -76,14 +120,10 @@ public function __construct($config)
76120 $ this ->bindOtherEvent ();
77121 }
78122
79- /**
80- * @description 此处借鉴 @laravel-s
81- * @link https://github.com/hhxsv5/laravel-s/blob/master/src/Swoole/Server.php#L65
82- */
83123 protected function bindBaseEvent ()
84124 {
85- $ this ->server ->on ('Start ' , [$ this , 'start ' ]);
86- $ this ->server ->on ('Shutdown ' , [$ this , 'shutdown ' ]);
125+ $ this ->server ->on ('Start ' , [$ this , 'Start ' ]);
126+ $ this ->server ->on ('Shutdown ' , [$ this , 'Shutdown ' ]);
87127 $ this ->server ->on ('ManagerStart ' , [$ this , 'ManagerStart ' ]);
88128 $ this ->server ->on ('ManagerStop ' , [$ this , 'ManagerStop ' ]);
89129 $ this ->server ->on ('WorkerStart ' , [$ this , 'WorkerStart ' ]);
@@ -108,45 +148,100 @@ protected function bindMasterEvent()
108148 $ eventHandler = $ websocketHandler ->make ();
109149
110150 $ this ->server ->on ('Open ' , function () use ($ eventHandler ) {
111- $ eventHandler ('open ' , func_get_args ());
151+ $ eventHandler ('Open ' , func_get_args ());
112152 });
113153
114154 $ this ->server ->on ('Message ' , function () use ($ eventHandler ) {
115- $ eventHandler ('message ' , func_get_args ());
155+ $ eventHandler ('Message ' , func_get_args ());
116156 });
117157
118158 $ this ->server ->on ('Close ' , function () use ($ eventHandler ) {
119- $ eventHandler ('close ' , func_get_args ());
159+ $ eventHandler ('Close ' , func_get_args ());
120160 });
121161 } else {
122162 $ httpHandler = new HttpHandler ($ handlerClass );
123163 $ eventHandler = $ httpHandler ->make ();
124164
125- $ this ->server ->on ('request ' , function () use ($ eventHandler ) {
126- $ eventHandler ('request ' , func_get_args ());
165+ $ this ->server ->on ('Request ' , function () use ($ eventHandler ) {
166+ $ eventHandler ('Request ' , func_get_args ());
127167 });
128168 }
129169 }
130170
131171 protected function bindOtherEvent ()
132172 {
173+ foreach ($ this ->otherConfig as $ serverName => $ event ) {
174+ $ setting = isset ($ event ['setting ' ]) ? $ event ['setting ' ] : [];
175+ if (!$ event ['enable ' ]) {
176+ continue ;
177+ }
178+
179+ if ($ serverName == 'http ' ) {
180+ if (!isset ($ setting ['daemonize ' ])) {
181+ $ setting ['daemonize ' ] = false ;
182+ }
183+
184+ $ setting ['open_http_protocol ' ] = true ;
185+ }
133186
187+ $ port = $ this ->server ->listen ($ event ['host ' ], $ event ['port ' ],
188+ $ event ['type ' ]);
189+ Logger::info ("开始监听 [ {$ serverName }] {$ event ['host ' ]}: {$ event ['port ' ]}" );
190+ if (!($ port instanceof \swoole_server_port)) {
191+ $ errno = method_exists ($ this ->server , 'getLastError ' )
192+ ? $ this ->server ->getLastError () : 'unknown ' ;
193+ $ errstr = sprintf ('listen %s:%s failed: errno=%s ' ,
194+ $ event ['host ' ], $ event ['port ' ], $ errno );
195+ Logger::err ($ errstr );
196+ continue ;
197+ }
198+
199+ $ port ->set ($ setting );
200+
201+ if (!isset ($ event ['handler ' ]) || !class_exists ($ event ['handler ' ])) {
202+ Logger::warn ("请创建 {$ serverName }的执行代码!!! " );
203+ continue ;
204+ }
205+
206+ $ handlerClass = $ event ['handler ' ];
207+
208+ $ serverHandler = new ServerHandler ($ port , $ handlerClass );
209+ $ eventHandler = $ serverHandler ->make ();
210+
211+ static $ events = [
212+ 'Open ' ,
213+ 'Request ' ,
214+ 'Message ' ,
215+ 'Connect ' ,
216+ 'Close ' ,
217+ 'Receive ' ,
218+ 'Packet ' ,
219+ 'BufferFull ' ,
220+ 'BufferEmpty ' ,
221+ ];
222+
223+ foreach ($ events as $ event ) {
224+ $ port ->on ($ event , function () use ($ event , $ eventHandler ) {
225+ $ eventHandler ($ event , func_get_args ());
226+ });
227+ }
228+ }
134229 }
135230
136- public function start (\swoole_http_server $ server )
231+ public function Start (\swoole_http_server $ server )
137232 {
138233 foreach (spl_autoload_functions () as $ function ) {
139234 spl_autoload_unregister ($ function );
140235 }
141236
142237 $ this ->setProcessName ('master process ' );
143238
144- if (version_compare (swoole_version (), '1.10.4 ' , '< ' )) {
239+ if (version_compare (\ swoole_version (), '1.10.4 ' , '>= ' )) {
145240 file_put_contents ($ this ->config ['pid_file ' ], $ server ->master_pid );
146241 }
147242 }
148243
149- public function shutdown (\swoole_http_server $ server )
244+ public function Shutdown (\swoole_http_server $ server )
150245 {
151246
152247 }
@@ -209,7 +304,7 @@ public function PipeMessage(
209304
210305 }
211306
212- public function task (
307+ public function Task (
213308 \swoole_http_server $ server ,
214309 $ task_id ,
215310 $ src_worker_id ,
@@ -223,7 +318,7 @@ function ($timer_id) {
223318 }
224319 }
225320
226- public function finish (\swoole_http_server $ server , $ task_id , $ data )
321+ public function Finish (\swoole_http_server $ server , $ task_id , $ data )
227322 {
228323
229324 }
0 commit comments