Skip to content

Commit d6e4152

Browse files
committed
fix redis connection close
1 parent 39c4baf commit d6e4152

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

GatewayWorker/Gateway.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -540,13 +540,16 @@ public function onWorkerMessage($connection, $data)
540540
break;
541541
// 发送数据给uid
542542
case GatewayProtocol::CMD_SEND_TO_UID:
543-
$uid = $data['ext_data'];
544-
if(!empty($this->_uidConnections[$uid]))
543+
$uid_array =json_decode($data['ext_data'],true);
544+
foreach($uid_array as $uid)
545545
{
546-
foreach($this->_uidConnections[$uid] as $connection)
547-
{
548-
$connection->send($data['body']);
549-
}
546+
if(!empty($this->_uidConnections[$uid]))
547+
{
548+
foreach($this->_uidConnections[$uid] as $connection)
549+
{
550+
$connection->send($data['body']);
551+
}
552+
}
550553
}
551554
break;
552555
default :

GatewayWorker/Lib/Gateway.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,22 @@ public static function bindUid($client_id, $uid)
188188

189189
/**
190190
* 向所有uid发送
191-
* @param unknown_type $uid
191+
* @param int/string/array $uid
192192
* @param unknown_type $message
193193
*/
194194
public static function sendToUid($uid, $message)
195195
{
196196
$gateway_data = GatewayProtocol::$empty;
197197
$gateway_data['cmd'] = GatewayProtocol::CMD_SEND_TO_UID;
198198
$gateway_data['body'] = $message;
199-
$gateway_data['ext_data'] = $uid;
199+
200+
if(!is_array($uid))
201+
{
202+
$uid = array($uid);
203+
}
204+
205+
$gateway_data['ext_data'] = json_encode($uid);
206+
200207
return self::sendToAllGateway($gateway_data);
201208
}
202209

GatewayWorker/Lib/Store.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,26 @@ public static function instance($config_name)
7676
{
7777
ini_set('default_socket_timeout',-1);
7878
self::$instance[$config_name] = new \GatewayWorker\Lib\StoreDriver\Redis();
79+
$config = \Config\Store::$$config_name;
7980
// 只选择第一个ip作为服务端
80-
$address = current(\Config\Store::$$config_name);
81+
$address = current($config);
8182
list($ip, $port) = explode(':', $address);
8283
$timeout = 1;
8384
self::$instance[$config_name]->connect($ip, $port, $timeout);
8485
self::$instance[$config_name]->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
86+
}else{
87+
try{
88+
self::$instance[$config_name]->ping();
89+
}catch (\RedisException $e){
90+
self::$instance[$config_name] = new \GatewayWorker\Lib\StoreDriver\Redis();
91+
$config = \Config\Store::$$config_name;
92+
// 只选择第一个ip作为服务端
93+
$address = current($config);
94+
list($ip, $port) = explode(':', $address);
95+
$timeout = 1;
96+
self::$instance[$config_name]->connect($ip, $port, $timeout);
97+
self::$instance[$config_name]->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
98+
}
8599
}
86100
return self::$instance[$config_name];
87101
}

Workerman/Connection/TcpConnection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@ public function baseRead($socket)
449449
// 包错误
450450
else
451451
{
452-
$this->close('error package. package_length='.var_export($this->_currentPackageLength, true));
452+
echo 'error package. package_length='.var_export($this->_currentPackageLength, true);
453+
$this->destroy();
453454
return;
454455
}
455456
}

0 commit comments

Comments
 (0)