我想讀取一個長時間運行的php進程,當滿足條件時會返回數據。閱讀長時間運行的PHP進程客戶端端
從我的研究,我碰到:
- 長輪詢
- 插座(socket.io &的node.js)
- 棘輪
我努力理解&應用我的問題的實施。我在PHP下面的循環:
public function store(ClientImpl $a)
{
$request = \Illuminate\Support\Facades\Request::all();
$originateMsg = new OriginateAction('Local/' . $request['agent'] . '@auto-answer');
$originateMsg->setContext('G-Outgoing');
$originateMsg->setPriority('1');
$originateMsg->setExtension($request['dial']);
$a->send($originateMsg);
while(true) {
if($a->process()) break;
usleep(1000);
}
$a->close();
echo 'OK';
ob_end_flush();
flush();
}
$a->process()
調用下面的方法:
/**
* Main processing loop. Also called from send(), you should call this in
* your own application in order to continue reading events and responses
* from ami.
*/
public function process()
{
$msgs = $this->getMessages();
foreach ($msgs as $aMsg) {
if ($this->_logger->isDebugEnabled()) {
$this->_logger->debug(
'------ Received: ------ ' . "\n" . $aMsg . "\n\n"
);
}
$resPos = strpos($aMsg, 'Response:');
$evePos = strpos($aMsg, 'Event:');
if (($resPos !== false) && (($resPos < $evePos) || $evePos === false)) {
$response = $this->_messageToResponse($aMsg);
$this->_incomingQueue[$response->getActionId()] = $response;
} else if ($evePos !== false) {
$event = $this->_messageToEvent($aMsg);
$response = $this->findResponse($event);
if ($response === false || $response->isComplete()) {
$this->dispatch($event);
} else {
$response->addEvent($event);
}
} else {
// broken ami.. sending a response with events without
// Event and ActionId
$bMsg = 'Event: ResponseEvent' . "\r\n";
$bMsg .= 'ActionId: ' . $this->_lastActionId . "\r\n" . $aMsg;
$event = $this->_messageToEvent($bMsg);
$response = $this->findResponse($event);
$response->addEvent($event);
}
if ($this->_logger->isDebugEnabled()) {
$this->_logger->debug('----------------');
}
}
}
$a->process()
然後疊出事件消息,閱讀這些,我創建的IEventListener
實現其也被稱爲「背後場景'當$a->process()
被調用。
class VoipEventStart implements IEventListener
{
public function handle(EventMessage $event)
{
$a = $event->getKeys();
if(($a['event'] == "Hangup" || $a['event'] == "HangupRequest") && strpos($a['channel'], 'SIP/') !== FALSE)
{
return true;
}
return false;
}
}
處理方法在用戶處於活動呼叫時從Asterisk PBX系統讀取事件。這意味着只要通話持續,處理循環就會持續。
如何在沒有瀏覽器看起來像加載/等待的情況下執行此客戶端?
你的意思是簡單的ajax? – Naruto
$ a-> process()是做什麼的?它運行多久?它返回什麼? – marekful
我沒有看到process()返回任何東西。但是當你的例子中的循環會在process()返回truthy時斷開/停止。 – marekful