Skip to content

Instantly share code, notes, and snippets.

@kachar
Last active January 3, 2016 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kachar/8434373 to your computer and use it in GitHub Desktop.
Save kachar/8434373 to your computer and use it in GitHub Desktop.
yii long polling
<?php
if(Yii::app()->request->isAjaxRequest) {
session_write_close();
$linesToLoad = $totalLines-$currentLine;
if($linesToLoad == 0) {
echo CJSON::encode(array(
'type' => 'no data',
'currentLine' => $currentLine,
"totalLines" => $totalLines,
"data" => array(),
));
} else {
$output = $this->tail($logFile, $linesToLoad);
echo CJSON::encode(array(
'type' => 'new data',
'currentLine' => $currentLine,
"totalLines" => $totalLines,
"data" => explode("\n", $output),
));
}
Yii::app()->end();
}
<?php
if(Yii::app()->request->isAjaxRequest) {
session_write_close();
$mtime = filemtime($logFile);
while(1) {
$totalLines = count(file($logFile));
$linesToLoad = $totalLines-$currentLine;
if($linesToLoad != 0) {
$output = $this->tail($logFile, $linesToLoad);
echo CJSON::encode(array(
'type' => 'new data',
'currentLine' => $currentLine,
"totalLines" => $totalLines,
"data" => explode("\n", $output),
));
Yii::app()->end();
}
$newmtime = filemtime($logFile);
if($mtime === $newmtime) {
sleep(1);
continue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment