18
18
use EasySwoole \Http \Request ;
19
19
use EasySwoole \Http \Response ;
20
20
use EasySwoole \Redis \Config \RedisConfig ;
21
- use EasySwoole \Session \FileSession ;
21
+ use EasySwoole \Session \FileSession as FileSessionHandler ;
22
22
use EasySwoole \Socket \Dispatcher ;
23
23
use EasySwoole \Template \Render ;
24
24
use EasySwoole \Utility \Random ;
@@ -195,8 +195,15 @@ public static function mainServerCreate(EventRegister $register)
195
195
$ conf = new \EasySwoole \Socket \Config ();
196
196
// 设置 Dispatcher 为 WebSocket 模式
197
197
$ conf ->setType (\EasySwoole \Socket \Config::WEB_SOCKET );
198
+
199
+ //解析器类
200
+ if (!empty ($ app_conf ['parser_class ' ])){
201
+ $ parser = $ app_conf ['parser_class ' ];
202
+ }else {
203
+ $ parser = WebSocketJsonParser::class;
204
+ }
198
205
// 设置解析器对象
199
- $ conf ->setParser (new WebSocketJsonParser () );
206
+ $ conf ->setParser (new $ parser );
200
207
// 创建 Dispatcher 对象 并注入 config 对象
201
208
$ dispatch = new Dispatcher ($ conf );
202
209
// 给server 注册相关事件 在 WebSocket 模式下 on message 事件必须注册 并且交给 Dispatcher 对象处理
@@ -208,14 +215,15 @@ public static function mainServerCreate(EventRegister $register)
208
215
/**
209
216
* **************** 启动Session **********************
210
217
*/
211
-
212
218
$ session_conf = config ('session ' );
213
219
if (!empty ($ session_conf ['enable ' ])){
214
220
//选择Session驱动
215
221
if ($ session_conf ['type ' ] == 'redis ' ){
216
222
$ handler = new \Sword \Component \Session \RedisSessionHandler ($ session_conf );
217
223
}elseif ($ session_conf ['type ' ] == 'file ' ){
218
- $ handler = new FileSession (EASYSWOOLE_TEMP_DIR . '/Session ' );
224
+ $ handler = new FileSessionHandler (EASYSWOOLE_TEMP_DIR . '/Session ' );
225
+ }else {
226
+ throw new \Exception ('未正确配置[session.type]. ' );
219
227
}
220
228
Session::getInstance ($ handler );
221
229
@@ -225,21 +233,28 @@ public static function mainServerCreate(EventRegister $register)
225
233
$ session_conf = config ('session ' );
226
234
$ sessName = $ session_conf ['session_name ' ];
227
235
236
+ $ sessionId = null ;
237
+ // 从Header中获取sessionId
238
+ if (!empty ($ session_conf ['enable_header ' ]) and $ param = $ request ->getHeader (strtolower ($ sessName ))){
239
+ $ sessionId = $ param [0 ];
240
+ }
241
+ // 从请求参数中获取sessionId
242
+ if (!$ sessionId and !empty ($ session_conf ['enable_param ' ]) and $ param = $ request ->getRequestParam ($ sessName )){
243
+ $ sessionId = $ param ;
244
+ }
228
245
// 从Cookie中获取sessionId
229
- $ cookie = $ request ->getCookieParams ($ sessName );
230
- // 从参数中获取sessionId
231
- if (!empty ($ session_conf ['enable_param ' ])){
232
- if ($ param = $ request ->getRequestParam ($ sessName )) $ cookie = $ param ;
246
+ if (!$ sessionId and $ cookie = $ request ->getCookieParams ($ sessName )){
247
+ $ sessionId = $ cookie ;
233
248
}
234
-
235
- if (!$ cookie ) {
236
- $ cookie = Random::character (32 ); // 生成 sessionId
237
- // 设置向客户端响应 Cookie 中 easy_session 参数
238
- $ response ->setCookie ($ sessName , $ cookie , time () + $ session_conf ['expire ' ]);
249
+ // 没有上传sessionId,创建新的会话
250
+ if (!$ sessionId ) {
251
+ $ sessionId = Random::character (32 ); // 生成sessionId
252
+ // 设置向客户端响应的Cookie参数
253
+ $ response ->setCookie ($ sessName , $ sessionId , time () + $ session_conf ['expire ' ]);
239
254
}
240
- // 存储 sessionId 方便调用 ,也可以通过其它方式存储
241
- $ request ->withAttribute ('sessionId ' , $ cookie );
242
- // Session::getInstance()->create($cookie );
255
+ // 存储sessionId方便调用 ,也可以通过其它方式存储
256
+ $ request ->withAttribute ('sessionId ' , $ sessionId );
257
+ // Session::getInstance()->create($sessionId );
243
258
}
244
259
});
245
260
Di::getInstance ()->set (SysConst::HTTP_GLOBAL_AFTER_REQUEST , function (Request $ request , Response $ response ) {
@@ -274,9 +289,13 @@ public static function logoSword()
274
289
$ es_v = SysConst::EASYSWOOLE_VERSION ;
275
290
$ t_d = EASYSWOOLE_TEMP_DIR ;
276
291
$ l_d = EASYSWOOLE_LOG_DIR ;
292
+
293
+ $ _port = (string ) config ('app.server_port ' );
294
+ $ _port = str_pad ('Port: ' . $ _port ,15 ," " ,STR_PAD_BOTH );
295
+
277
296
echo <<<LOGO
278
297
_____ _
279
- / ____| | | PHP \e[34mv {$ p_v }\e[0m
298
+ / ____| \e [33m { $ _port }\e [0m | | PHP \e[34mv {$ p_v }\e[0m
280
299
| (_____ _____ _ __ __| | Swoole \e[34mv {$ s_v }\e[0m
281
300
\___ \ \ /\ / / _ \| '__/ _` | Temp Dir \e[34m {$ t_d }\e[0m
282
301
____) \ V V | (_) | | | (_| | Log Dir \e[34m {$ l_d }\e[0m
0 commit comments