Gatewayworker中使用AsyncTcpConnection如何保活连接?

vall_lai

我在本地的Gatewayworker中第一个businessWorker线程建了一个AsyncTcpConnection连接,如果只在onclose()中做reConnect(5)的话,发现当我拔掉网线的时候没办法重连到远端,所以做了心跳,开了个定时器监测,超过2次就直接重新连接,但是运行一段时间后发现AsyncTcpConnection一直反复的执行到onclose(),这是什么原因?或者说我用的方法有问题?有没有这方面的demo?

3009 2 0
2个回答

walkor

可能是逻辑有问题,发下代码看看?

  • 暂无评论
vall_lai

大致是这样的,有些不相关的我删掉了

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{

        }
}
  • veinliu 2019-11-22

    能把怎么把AsyncTcpConnection集成到businessWorker里面的代码发给我参考一下吗?我研究了一天不能用。

年代过于久远,无法发表回答
🔝