buildWsAddress(); // ssl需要访问443端口 $con = new AsyncTcpConnection($address); // 设置以ssl加密方式访问,使之成为wss if (!AppConst::isDuoDuoCloud()) { $con->transport = 'ssl'; } $con->onConnect = function($con) use ($pddMessageService) { $pddMessageService->log('onConnect'); }; $con->onMessage = function($con, $data) use ($pddMessageService) { $pddMessageService->log('onMessage:' . $data); $retMsg = $pddMessageService->onPmcMessage($data); if ($retMsg !== false) { $con->send($retMsg); } }; $con->onClose = function($con) use ($pddMessageService) { $pddMessageService->log('onClose'); }; $con->connect(); return $con; } $worker->onWorkerStart = function($worker) use ($pddMessageService) { global $con; $con = connect($pddMessageService); $time_interval = 10; Timer::add($time_interval, function() use($pddMessageService) { global $con; if (empty($con)) { $pddMessageService->log('timer connect is null'); return; } $currentStatus = $con->getStatus(false); $pddMessageService->log('timer:' . $currentStatus); if (!in_array($currentStatus, ['ESTABLISHED', 'CONNECTING' ,'CLOSING'])) { $pddMessageService->log('reconnect'); $con = connect($pddMessageService); } else if ($currentStatus == 'ESTABLISHED'){ $ret = $con->send($pddMessageService->buildHeartBeatMessage()); $pddMessageService->log('send heartbeat ret' . print_r($ret, true)); } }); }; Worker::runAll();