forked from ccoenen/server-sent-events-demo
-
Notifications
You must be signed in to change notification settings - Fork 4
/
source.php
36 lines (28 loc) · 764 Bytes
/
source.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
/*
Server-Sent Events / EventSource DEMO
Claudius Coenen
This is free software. Use, modify and tinker with it however you like!
LICENSED UNDER CC-BY-4.0 http://creativecommons.org/licenses/by/4.0/
*/
error_log('script starting');
header('Content-Type: text/event-stream');
header('Access-Control-Allow-Origin: *');
header('Cache-Control: no-cache');
echo "retry: 3000\n";
$seed = rand();
$count = 0;
function sendMsg($msg) {
global $count, $seed;
error_log('message sent: ' . $msg);
echo "event: testeventcc\n";
echo "data: $msg $count $seed\n\n";
ob_flush();
flush();
}
while (!connection_aborted()) {
$count++;
sendMsg('server time: ' . date("H:i:s"));
usleep(16000); // 16ms per message -> 60msg/s
}
error_log("connection closed");