Last active
April 10, 2016 14:15
-
-
Save clouddueling/6296036 to your computer and use it in GitHub Desktop.
Long polling class
This file contains 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
public function get_check($queue_id = 0) | |
{ | |
$options = array( | |
'queue_id' => $queue_id | |
); | |
$status = Polling::forResult(function($options) { | |
$queue = Queue::find($options['queue_id']); | |
has_access($queue); | |
if ($queue->status == 'completed') | |
return true; | |
return false; | |
}, $options, 1000, 4); | |
$data = array( | |
'status' => $status | |
); | |
return json($data); | |
} |
This file contains 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
public function get_waiting() | |
{ | |
$contact = Polling::forResult(function(){ | |
$contact = Contact::where_account_user_id(Auth::user()->account_user_id) | |
->where_request(1) | |
->first(); | |
if ($contact != null) { | |
return $contact->to_array(); | |
} | |
return false; | |
}); | |
return json($contact); | |
} |
This file contains 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