Skip to content

Instantly share code, notes, and snippets.

@clouddueling
Last active December 30, 2015 17:09
Show Gist options
  • Save clouddueling/7859284 to your computer and use it in GitHub Desktop.
Save clouddueling/7859284 to your computer and use it in GitHub Desktop.
long polling in php
Polling::forResult(function($options) {
if ($options['key'] == 'value') {
// end polling
return true;
} else {
// continue polling
return false;
}
}, ['key' => 'value']);
<?php
class Polling
{
public static function forResult($callback, $options = array(), $max_cycles = 35, $sleep = 2, $failed = false)
{
$thread = uniqid();
$cycles = 0;
while (1) {
++$cycles;
$result = $callback($options);
if ($result !== false)
return $result;
if ($cycles >= $max_cycles)
return $failed;
sleep($sleep);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment