无限重连问题

 
$worker = new Worker();

$worker->onWorkerStart = function ($worker) {

//a客户端
$con = new AsyncTcpConnection('ws:/aaa.com');
$con->onConnect = function ($con) {
echo '=连接=';
$con->lastMessageTime = time();

//心跳
$time_interval = 5;
$con->timer_id = Timer::add($time_interval, function () use ($con) {
$stamp = time();
//断线重连
if (($stamp - $con->lastMessageTime) > 7) {
echo '=断线重连=';
Timer::del($con->timer_id);
$con->close();
} else {
$con->send('{"ping": '. $stamp .'}');
}
});
};

$con->onMessage = function ($con, $data) {
$data = json_decode($data, true);
if (isset($data['pong'])) {
$con->lastMessageTime = $data['pong'];
return;
}
};

$con->onClose = function ($con) {
echo '=关闭=';
if (isset($con->timer_id)) {
Timer::del($con->timer_id);
}
$con->reConnect(2);
};

$con->connect();

//b客户端
$cons = new AsyncTcpConnection('ws:/bbb.com');
$cons->onConnect = function ($cons) {
echo '=连接s=';
$cons->lastMessageTime = time();

//心跳
$time_interval = 5;
$cons->timer_id = Timer::add($time_interval, function () use ($cons) {
$stamp = time();
//断线重连
if (($stamp - $cons->lastMessageTime) > 7) {
echo '=断线重连s=';
Timer::del($cons->timer_id);
$cons->close();
} else {
$cons->send('{"ping": '. $stamp .'}');
}
});
};

$cons->onMessage = function ($cons, $data) {
$data = json_decode($data, true);
if (isset($data['pong'])) {
$cons->lastMessageTime = $data['pong'];
return;
}
};

$cons->onClose = function ($cons) {
echo '=关闭s=';
if (isset($cons->timer_id)) {
Timer::del($cons->timer_id);
}
$cons->reConnect(2);
};

$cons->connect();
};

Worker::runAll();

运行一段时候,会出现下图中的无限断线重连问题,客户端a和b都有出现过这个问题,实在想不通,求助
 
已邀请:

blogdaren - 乐于分享的PHP码农【http://www.blogdaren.com】

赞同来自:

1、if ($stamp - $cons->lastMessageTime) > 7,  定时器第二次执行的时候肯定满足这个条件了;
2、另外根据场景:
$con->lastMessageTime = time(); 
应该改为:
empty($con->lastMessageTime) && $con->lastMessageTime = time();

walkor

赞同来自:

更新到最新版本3.5.19试下,3.5.19版本优化了下重连问题

2007lf

赞同来自:

WX20190602-231600@2x.png

Workerman-3.5.19/Worker.php

2007lf

赞同来自:

最近又出现无限重连的情况,中间有段时候稳定了很久,非常奇怪,=连接s= 中,没有断的情况,又来了=连接s=,然后就无限重连,=连接= 都正常,就我上面这种多个客户端放到一个进程中是否合理?@walkor 请教帮助,谢谢!

要回复问题请先登录注册