Skip to content

Commit 3e077ab

Browse files
committed
优化Session功能
1 parent 17c4e4e commit 3e077ab

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

src/Sword.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010

1111
class Sword
1212
{
13-
const VERSION = '0.2.3';
13+
const VERSION = '0.2.4';
1414
}

src/SwordEvent.php

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use EasySwoole\Http\Request;
1919
use EasySwoole\Http\Response;
2020
use EasySwoole\Redis\Config\RedisConfig;
21-
use EasySwoole\Session\FileSession;
21+
use EasySwoole\Session\FileSession as FileSessionHandler;
2222
use EasySwoole\Socket\Dispatcher;
2323
use EasySwoole\Template\Render;
2424
use EasySwoole\Utility\Random;
@@ -195,8 +195,15 @@ public static function mainServerCreate(EventRegister $register)
195195
$conf = new \EasySwoole\Socket\Config();
196196
// 设置 Dispatcher 为 WebSocket 模式
197197
$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+
}
198205
// 设置解析器对象
199-
$conf->setParser(new WebSocketJsonParser());
206+
$conf->setParser(new $parser);
200207
// 创建 Dispatcher 对象 并注入 config 对象
201208
$dispatch = new Dispatcher($conf);
202209
// 给server 注册相关事件 在 WebSocket 模式下 on message 事件必须注册 并且交给 Dispatcher 对象处理
@@ -208,14 +215,15 @@ public static function mainServerCreate(EventRegister $register)
208215
/**
209216
* **************** 启动Session **********************
210217
*/
211-
212218
$session_conf = config('session');
213219
if(!empty($session_conf['enable'])){
214220
//选择Session驱动
215221
if($session_conf['type'] == 'redis'){
216222
$handler = new \Sword\Component\Session\RedisSessionHandler($session_conf);
217223
}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].');
219227
}
220228
Session::getInstance($handler);
221229

@@ -225,21 +233,28 @@ public static function mainServerCreate(EventRegister $register)
225233
$session_conf = config('session');
226234
$sessName = $session_conf['session_name'];
227235

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+
}
228245
// 从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;
233248
}
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']);
239254
}
240-
// 存储 sessionId 方便调用,也可以通过其它方式存储
241-
$request->withAttribute('sessionId', $cookie);
242-
// Session::getInstance()->create($cookie);
255+
// 存储sessionId方便调用,也可以通过其它方式存储
256+
$request->withAttribute('sessionId', $sessionId);
257+
// Session::getInstance()->create($sessionId);
243258
}
244259
});
245260
Di::getInstance()->set(SysConst::HTTP_GLOBAL_AFTER_REQUEST, function (Request $request, Response $response) {
@@ -274,9 +289,13 @@ public static function logoSword()
274289
$es_v = SysConst::EASYSWOOLE_VERSION;
275290
$t_d = EASYSWOOLE_TEMP_DIR;
276291
$l_d = EASYSWOOLE_LOG_DIR;
292+
293+
$_port = (string) config('app.server_port');
294+
$_port = str_pad('Port:'. $_port,15," ",STR_PAD_BOTH);
295+
277296
echo <<<LOGO
278297
_____ _
279-
/ ____| | | PHP \e[34mv{$p_v}\e[0m
298+
/ ____| \e[33m{$_port}\e[0m | | PHP \e[34mv{$p_v}\e[0m
280299
| (_____ _____ _ __ __| | Swoole \e[34mv{$s_v}\e[0m
281300
\___ \ \ /\ / / _ \| '__/ _` | Temp Dir \e[34m{$t_d}\e[0m
282301
____) \ V V | (_) | | | (_| | Log Dir \e[34m{$l_d}\e[0m

0 commit comments

Comments
 (0)