Last active
September 7, 2017 01:32
-
-
Save liuxd/8090948 to your computer and use it in GitHub Desktop.
[EventSource.php] PHP实现EventSource服务端。
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 | |
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