Last active
December 30, 2015 17:09
-
-
Save clouddueling/7859284 to your computer and use it in GitHub Desktop.
long polling in php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Polling::forResult(function($options) { | |
if ($options['key'] == 'value') { | |
// end polling | |
return true; | |
} else { | |
// continue polling | |
return false; | |
} | |
}, ['key' => 'value']); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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