-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream4.php
More file actions
60 lines (50 loc) · 1.21 KB
/
stream4.php
File metadata and controls
60 lines (50 loc) · 1.21 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
header("X-Accel-Buffering: no");
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
$pipe = "/tmp/pipe2.pipe";
$f = fopen($pipe, "r");
if (!$f) {
echo "data: Failed to open pipe!\n\n";
flush();
exit;
}
//exec('sleep 10 > /dev/null &; echo "test14" > /tmp/pipe2.pipe > /dev/null &');
$data = '';
while (true) {
// Every min, send a "ping" event.
echo "event: ping\n\n";
$listen=true;
$i=10;
while($i > 0)
{
$chunk = fread($f, 1024);
if ($chunk !== false && strlen($chunk) > 0) {
$data .= $chunk;
unset($chunk);
$listen=false;
}
//usleep(200000); // sleep 200ms before trying again
sleep(1);
$i--;
//$listen=false;
//fclose($f);
}
if (strlen(trim($data)) > 0) {
//echo "retry: 120000\n";
//echo "event: update\n";
//using the data tag here mans grouped messages get misseed.
echo "data: " . trim($data) . "\n\n";
$listen=true;
$data = '';
}
if (ob_get_contents()) {
ob_end_flush();
}
flush();
// Break the loop if the client aborted the connection (closed the page)
if (connection_aborted()) break;
//sleep(3);
}
fclose($f);
?>