Skip to content

Instantly share code, notes, and snippets.

@darfink
Created October 2, 2014 15:53
Show Gist options
  • Save darfink/90cf692d021a88de9719 to your computer and use it in GitHub Desktop.
Save darfink/90cf692d021a88de9719 to your computer and use it in GitHub Desktop.
function eventSourceTest() {
header("Content-Type: text/event-stream\n\n");
$counter = rand(1, 10);
echo "Bajs $counter eaaa";
while (1) {
// Every second, sent a "ping" event.
echo "event: ping\n";
$curDate = @date(DATE_ISO8601);
echo 'data: {"time": "' . $curDate . '"}';
echo "\n\n";
// Send a simple message at random intervals.
$counter--;
if (!$counter) {
echo 'data: This is a message at time ' . $curDate . "\n\n";
$counter = rand(1, 10);
}
ob_flush();
flush();
sleep(1);
}
exit();
}
add_action('wp_ajax_test', 'eventSourceTest');
add_action('wp_ajax_nopriv_test', 'eventSourceTest');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment