我在本地的Gatewayworker中第一個(gè)businessWorker線程建了一個(gè)AsyncTcpConnection連接,如果只在onclose()中做reConnect(5)的話,發(fā)現(xiàn)當(dāng)我拔掉網(wǎng)線的時(shí)候沒辦法重連到遠(yuǎn)端,所以做了心跳,開了個(gè)定時(shí)器監(jiān)測(cè),超過2次就直接重新連接,但是運(yùn)行一段時(shí)間后發(fā)現(xiàn)AsyncTcpConnection一直反復(fù)的執(zhí)行到onclose(),這是什么原因?或者說我用的方法有問題?有沒有這方面的demo?
大致是這樣的,有些不相關(guān)的我刪掉了
if ($businessWorker->id == 0) {
Timer::add(60,'re',true);
$GLOBALS->connect();
$GLOBALS->onClose = function($connection_to_yun) {
$GLOBALS->reConnect(5);
$GLOBALS = strtotime(date("Y-m-d H:i:s"));
};
$GLOBALS->onMessage = function($connection_to_yun, $message){
if($message == "0000FFFF")
//心跳
heart();
}
}
}
function heart(){
$heartbeat_time=date("Y-m-d H:i:s");
$GLOBALS=strtotime($heartbeat_time);
$GLOBALS->send("1111");
}
//重連
function re(){
$isNetworkReachable = varify_url('http://xxxxxxxxxx');
$currentTime = strtotime(date("Y-m-d H:i:s"));
if($currentTime - $GLOBALS>120 && $isNetworkReachable == "1"){
$GLOBALS->reConnect(5);
}else{
}
}