Skip to content

Commit

Permalink
fix redis connection close
Browse files Browse the repository at this point in the history
  • Loading branch information
walkor committed Sep 1, 2015
1 parent 39c4baf commit d6e4152
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
15 changes: 9 additions & 6 deletions GatewayWorker/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,13 +540,16 @@ public function onWorkerMessage($connection, $data)
break;
// 发送数据给uid
case GatewayProtocol::CMD_SEND_TO_UID:
$uid = $data['ext_data'];
if(!empty($this->_uidConnections[$uid]))
$uid_array =json_decode($data['ext_data'],true);
foreach($uid_array as $uid)
{
foreach($this->_uidConnections[$uid] as $connection)
{
$connection->send($data['body']);
}
if(!empty($this->_uidConnections[$uid]))
{
foreach($this->_uidConnections[$uid] as $connection)
{
$connection->send($data['body']);
}
}
}
break;
default :
Expand Down
11 changes: 9 additions & 2 deletions GatewayWorker/Lib/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,22 @@ public static function bindUid($client_id, $uid)

/**
* 向所有uid发送
* @param unknown_type $uid
* @param int/string/array $uid
* @param unknown_type $message
*/
public static function sendToUid($uid, $message)
{
$gateway_data = GatewayProtocol::$empty;
$gateway_data['cmd'] = GatewayProtocol::CMD_SEND_TO_UID;
$gateway_data['body'] = $message;
$gateway_data['ext_data'] = $uid;

if(!is_array($uid))
{
$uid = array($uid);
}

$gateway_data['ext_data'] = json_encode($uid);

return self::sendToAllGateway($gateway_data);
}

Expand Down
16 changes: 15 additions & 1 deletion GatewayWorker/Lib/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,26 @@ public static function instance($config_name)
{
ini_set('default_socket_timeout',-1);
self::$instance[$config_name] = new \GatewayWorker\Lib\StoreDriver\Redis();
$config = \Config\Store::$$config_name;
// 只选择第一个ip作为服务端
$address = current(\Config\Store::$$config_name);
$address = current($config);
list($ip, $port) = explode(':', $address);
$timeout = 1;
self::$instance[$config_name]->connect($ip, $port, $timeout);
self::$instance[$config_name]->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
}else{
try{
self::$instance[$config_name]->ping();
}catch (\RedisException $e){
self::$instance[$config_name] = new \GatewayWorker\Lib\StoreDriver\Redis();
$config = \Config\Store::$$config_name;
// 只选择第一个ip作为服务端
$address = current($config);
list($ip, $port) = explode(':', $address);
$timeout = 1;
self::$instance[$config_name]->connect($ip, $port, $timeout);
self::$instance[$config_name]->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
}
}
return self::$instance[$config_name];
}
Expand Down
3 changes: 2 additions & 1 deletion Workerman/Connection/TcpConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@ public function baseRead($socket)
// 包错误
else
{
$this->close('error package. package_length='.var_export($this->_currentPackageLength, true));
echo 'error package. package_length='.var_export($this->_currentPackageLength, true);
$this->destroy();
return;
}
}
Expand Down

0 comments on commit d6e4152

Please sign in to comment.