]>
Commit | Line | Data |
---|---|---|
fd4848e4 | 1 | <?php |
c06c1713 | 2 | include "../inc/common.php"; |
fd4848e4 | 3 | |
9c9d2cdf BM |
4 | if(session_status() !== PHP_SESSION_ACTIVE) session_start(); |
5 | ||
fd4848e4 BM |
6 | if (!isset($_SESSION['id'])) |
7 | die("Access denied"); | |
8 | ||
9 | // Close the session now, otherwise other pages block | |
10 | session_write_close(); | |
11 | ||
12 | // Only now make the connection (this can take a short while) | |
c06c1713 | 13 | include "../inc/connection.php"; |
fd4848e4 BM |
14 | |
15 | // Server Side Events | |
63026197 BM |
16 | if (!defined('NO_EVENT_STREAM_HEADER')) |
17 | header('Content-Type: text/event-stream'); | |
fd4848e4 BM |
18 | |
19 | // Explicitly disable caching so Varnish and other upstreams won't cache. | |
20 | header("Cache-Control: no-cache, must-revalidate"); | |
21 | ||
22 | // Setting this header instructs Nginx to disable fastcgi_buffering and disable | |
23 | // gzip for this request. | |
24 | header('X-Accel-Buffering: no'); | |
25 | ||
26 | // No time limit | |
27 | set_time_limit(0); | |
28 | ||
29 | // Send content immediately | |
30 | ob_implicit_flush(1); | |
31 | ||
f9558c92 BM |
32 | // Flush and stop output buffering (eg fastcgi w/NGINX) |
33 | while (@ob_end_flush()); | |
fd4848e4 BM |
34 | |
35 | /* Send server-sent events (SSE) message */ | |
36 | function send_sse($json) | |
37 | { | |
38 | echo "data: ".json_encode($json)."\n\n"; | |
39 | } | |
40 | ||
41 | function api_log_loop($sources) | |
42 | { | |
43 | GLOBAL $rpc; | |
44 | ||
45 | $rpc->log()->subscribe($sources); | |
46 | if ($rpc->error) | |
47 | { | |
48 | echo $rpc->error; | |
49 | die; | |
50 | } | |
51 | ||
52 | for(;;) | |
53 | { | |
54 | $res = $rpc->eventloop(); | |
55 | if (!$res) | |
0a2b3443 BM |
56 | { |
57 | /* Output at least something every timeout (10) seconds, | |
58 | * otherwise PHP may not | |
59 | * notice when the webclient is gone. | |
60 | */ | |
61 | echo "\n"; | |
fd4848e4 | 62 | continue; |
0a2b3443 | 63 | } |
fd4848e4 BM |
64 | send_sse($res); |
65 | } | |
66 | } | |
67 | ||
68 | function api_timer_loop(int $every_msec, string $method, array|null $params = null) | |
69 | { | |
70 | GLOBAL $rpc; | |
71 | ||
72 | $rpc->rpc()->add_timer("timer", $every_msec, $method, $params); | |
73 | if ($rpc->error) | |
74 | { | |
69e00a0c BM |
75 | /* Have to resort to old style: client-side timer */ |
76 | while(1) | |
77 | { | |
78 | $res = $rpc->query($method, $params); | |
79 | if (!$res) | |
80 | die; | |
81 | send_sse($res); | |
563ebce7 | 82 | usleep($every_msec * 1000); |
69e00a0c | 83 | } |
fd4848e4 BM |
84 | } |
85 | ||
69e00a0c | 86 | /* New style: use server-side timers */ |
fd4848e4 BM |
87 | for(;;) |
88 | { | |
89 | $res = $rpc->eventloop(); | |
90 | if (!$res) | |
0a2b3443 BM |
91 | { |
92 | /* Output at least something every timeout (10) seconds, | |
93 | * otherwise PHP may not | |
94 | * notice when the webclient is gone. | |
95 | */ | |
96 | echo "\n"; | |
fd4848e4 | 97 | continue; |
0a2b3443 | 98 | } |
fd4848e4 BM |
99 | send_sse($res); |
100 | } | |
101 | } |