Skip to content

Commit

Permalink
Check if the session is old
Browse files Browse the repository at this point in the history
  • Loading branch information
walkor authored Jun 20, 2017
1 parent d1f654a commit 7f04aca
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/BusinessWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Workerman\Connection\AsyncTcpConnection;
use GatewayWorker\Protocols\GatewayProtocol;
use GatewayWorker\Lib\Context;
use GatewayWorker\Lib\Gateway;

/**
*
Expand Down Expand Up @@ -149,6 +150,13 @@ class BusinessWorker extends Worker
*/
protected $_eventOnClose = null;

/**
* SESSION 版本缓存
*
* @var array
*/
protected $_sessionVersion = array();

/**
* 用于保持长连接的心跳时间间隔
*
Expand Down Expand Up @@ -347,11 +355,19 @@ public function onGatewayMessage($connection, $data)
'GATEWAY_PORT' => $data['gateway_port'],
'GATEWAY_CLIENT_ID' => Context::$client_id,
);
// 尝试解析 session
if ($data['ext_data'] != '') {
Context::$old_session = $_SESSION = Context::sessionDecode($data['ext_data']);
// 检查session版本,如果是过期的session数据则拉取最新的数据
if (isset($this->_sessionVersion[Context::$client_id]) && $this->_sessionVersion[Context::$client_id] !== crc32($data['ext_data'])) {
$_SESSION = Context::$old_session = Gateway::getSession(Context::$client_id);
} else {
Context::$old_session = $_SESSION = null;
if (!isset($this->_sessionVersion[Context::$client_id])) {
$this->_sessionVersion[Context::$client_id] = crc32($data['ext_data']);
}
// 尝试解析 session
if ($data['ext_data'] != '') {
Context::$old_session = $_SESSION = Context::sessionDecode($data['ext_data']);
} else {
Context::$old_session = $_SESSION = null;
}
}

if ($this->processTimeout) {
Expand All @@ -370,6 +386,7 @@ public function onGatewayMessage($connection, $data)
}
break;
case GatewayProtocol::CMD_ON_CLOSE:
unset($this->_sessionVersion[Context::$client_id]);
if ($this->_eventOnClose) {
call_user_func($this->_eventOnClose, Context::$client_id);
}
Expand All @@ -385,9 +402,10 @@ public function onGatewayMessage($connection, $data)
}

// 判断 session 是否被更改
if ($_SESSION !== Context::$old_session) {
if ($_SESSION !== Context::$old_session && $cmd !== GatewayProtocol::CMD_ON_CLOSE) {
$session_str_now = $_SESSION !== null ? Context::sessionEncode($_SESSION) : '';
\GatewayWorker\Lib\Gateway::setSocketSession(Context::$client_id, $session_str_now);
$this->_sessionVersion[Context::$client_id] = crc32($session_str_now);
}

Context::clear();
Expand Down

0 comments on commit 7f04aca

Please sign in to comment.