5
5
6
6
use swoole_websocket_and_tcp_and_udp \common \Logger ;
7
7
use swoole_websocket_and_tcp_and_udp \common \ProcessTrait ;
8
+ use swoole_websocket_and_tcp_and_udp \common \Swoole ;
8
9
use swoole_websocket_and_tcp_and_udp \handler \HttpHandler ;
10
+ use swoole_websocket_and_tcp_and_udp \handler \ServerHandler ;
9
11
use swoole_websocket_and_tcp_and_udp \handler \WebsocketHandler ;
10
12
13
+ /**
14
+ * @description 此文件多处借鉴 @laravel-s
15
+ * @link https://github.com/hhxsv5/laravel-s/blob/master/src/Swoole/Server.php#L65
16
+ */
11
17
class Server
12
18
{
13
- use ProcessTrait, Logger;
19
+ use ProcessTrait, Logger, Swoole ;
14
20
15
21
protected $ port ;
16
22
@@ -27,34 +33,71 @@ class Server
27
33
28
34
protected $ enableTask = false ;
29
35
36
+ protected $ otherConfig = [];
37
+
30
38
public function __construct ($ config )
31
39
{
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
+
32
54
$ this ->config = $ config ;
33
55
ini_set ('date.timezone ' , $ this ->config ['timezone ' ]);
34
56
35
57
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;
44
74
}
75
+ }
45
76
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 ' ]) {
49
79
$ this ->primaryConfig = $ this ->config ['http ' ];
50
80
$ this ->primaryConfig ['setting ' ]['open_http_protocol ' ] = true ;
51
- if (!isset ($ this ->primaryConfig ['setting ' ]['daemonize ' ])) {
81
+ if (!isset ($ this ->primaryConfig ['setting ' ]['daemonize ' ])) {
52
82
$ this ->primaryConfig ['setting ' ]['daemonize ' ] = false ;
53
83
}
54
-
55
84
$ 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 ;
56
96
}
97
+
98
+ $ this ->otherConfig [$ name ] = $ item ;
57
99
}
100
+ // var_dump($this->otherConfig);exit(1);
58
101
59
102
$ host = $ this ->primaryConfig ['host ' ];
60
103
$ port = $ this ->primaryConfig ['port ' ];
@@ -65,7 +108,8 @@ public function __construct($config)
65
108
$ this ->enableTask = true ;
66
109
}
67
110
68
- Logger::info ("开始监听端口 {$ host }: {$ port }" );
111
+ $ serverName = $ this ->enableWebsocket ?'websocket ' :'http ' ;
112
+ Logger::info ("开始监听 [ {$ serverName }] {$ host }: {$ port }" );
69
113
$ this ->server = new $ serverClass ($ host , $ port , SWOOLE_PROCESS , $ type );
70
114
$ this ->server ->set ($ setting );
71
115
@@ -76,14 +120,10 @@ public function __construct($config)
76
120
$ this ->bindOtherEvent ();
77
121
}
78
122
79
- /**
80
- * @description 此处借鉴 @laravel-s
81
- * @link https://github.com/hhxsv5/laravel-s/blob/master/src/Swoole/Server.php#L65
82
- */
83
123
protected function bindBaseEvent ()
84
124
{
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 ' ]);
87
127
$ this ->server ->on ('ManagerStart ' , [$ this , 'ManagerStart ' ]);
88
128
$ this ->server ->on ('ManagerStop ' , [$ this , 'ManagerStop ' ]);
89
129
$ this ->server ->on ('WorkerStart ' , [$ this , 'WorkerStart ' ]);
@@ -108,45 +148,100 @@ protected function bindMasterEvent()
108
148
$ eventHandler = $ websocketHandler ->make ();
109
149
110
150
$ this ->server ->on ('Open ' , function () use ($ eventHandler ) {
111
- $ eventHandler ('open ' , func_get_args ());
151
+ $ eventHandler ('Open ' , func_get_args ());
112
152
});
113
153
114
154
$ this ->server ->on ('Message ' , function () use ($ eventHandler ) {
115
- $ eventHandler ('message ' , func_get_args ());
155
+ $ eventHandler ('Message ' , func_get_args ());
116
156
});
117
157
118
158
$ this ->server ->on ('Close ' , function () use ($ eventHandler ) {
119
- $ eventHandler ('close ' , func_get_args ());
159
+ $ eventHandler ('Close ' , func_get_args ());
120
160
});
121
161
} else {
122
162
$ httpHandler = new HttpHandler ($ handlerClass );
123
163
$ eventHandler = $ httpHandler ->make ();
124
164
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 ());
127
167
});
128
168
}
129
169
}
130
170
131
171
protected function bindOtherEvent ()
132
172
{
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
+ }
133
186
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
+ }
134
229
}
135
230
136
- public function start (\swoole_http_server $ server )
231
+ public function Start (\swoole_http_server $ server )
137
232
{
138
233
foreach (spl_autoload_functions () as $ function ) {
139
234
spl_autoload_unregister ($ function );
140
235
}
141
236
142
237
$ this ->setProcessName ('master process ' );
143
238
144
- if (version_compare (swoole_version (), '1.10.4 ' , '< ' )) {
239
+ if (version_compare (\ swoole_version (), '1.10.4 ' , '>= ' )) {
145
240
file_put_contents ($ this ->config ['pid_file ' ], $ server ->master_pid );
146
241
}
147
242
}
148
243
149
- public function shutdown (\swoole_http_server $ server )
244
+ public function Shutdown (\swoole_http_server $ server )
150
245
{
151
246
152
247
}
@@ -209,7 +304,7 @@ public function PipeMessage(
209
304
210
305
}
211
306
212
- public function task (
307
+ public function Task (
213
308
\swoole_http_server $ server ,
214
309
$ task_id ,
215
310
$ src_worker_id ,
@@ -223,7 +318,7 @@ function ($timer_id) {
223
318
}
224
319
}
225
320
226
- public function finish (\swoole_http_server $ server , $ task_id , $ data )
321
+ public function Finish (\swoole_http_server $ server , $ task_id , $ data )
227
322
{
228
323
229
324
}
0 commit comments