Skip to content

Instantly share code, notes, and snippets.

@liuxd
Last active September 7, 2017 01:32
Show Gist options
  • Save liuxd/8090948 to your computer and use it in GitHub Desktop.
Save liuxd/8090948 to your computer and use it in GitHub Desktop.
[EventSource.php] PHP实现EventSource服务端。
<?php
header("Access-Control-Allow-Origin: *");
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
function esFlush($msg){
echo "data: ", $msg, "\n\n";
flush();
}
function lastEventId(){
$lastEventId = floatval(isset($_SERVER["HTTP_LAST_EVENT_ID"]) ? $_SERVER["HTTP_LAST_EVENT_ID"] : 0);
if ($lastEventId == 0) {
$lastEventId = floatval(isset($_GET["lastEventId"]) ? $_GET["lastEventId"] : 0);
}
}
lastEventId = lastEventId();
esFlush('Hello EventSource! + '.$lastEventId);
# end of this file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment