]>
Commit | Line | Data |
---|---|---|
fd4848e4 BM |
1 | <?php |
2 | include "../common.php"; | |
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) | |
13 | include "../connection.php"; | |
14 | ||
15 | // Server Side Events | |
16 | header('Content-Type: text/event-stream'); | |
17 | ||
18 | // Explicitly disable caching so Varnish and other upstreams won't cache. | |
19 | header("Cache-Control: no-cache, must-revalidate"); | |
20 | ||
21 | // Setting this header instructs Nginx to disable fastcgi_buffering and disable | |
22 | // gzip for this request. | |
23 | header('X-Accel-Buffering: no'); | |
24 | ||
25 | // No time limit | |
26 | set_time_limit(0); | |
27 | ||
28 | // Send content immediately | |
29 | ob_implicit_flush(1); | |
30 | ||
31 | // Eh.. yeah... | |
32 | ob_end_flush(); | |
33 | ||
34 | // If we use fastcgi, then finish the request now (UNTESTED) | |
35 | if (function_exists('fastcgi_finish_request')) | |
36 | fastcgi_finish_request(); | |
37 | ||
38 | /* Send server-sent events (SSE) message */ | |
39 | function send_sse($json) | |
40 | { | |
41 | echo "data: ".json_encode($json)."\n\n"; | |
42 | } | |
43 | ||
44 | function api_log_loop($sources) | |
45 | { | |
46 | GLOBAL $rpc; | |
47 | ||
48 | $rpc->log()->subscribe($sources); | |
49 | if ($rpc->error) | |
50 | { | |
51 | echo $rpc->error; | |
52 | die; | |
53 | } | |
54 | ||
55 | for(;;) | |
56 | { | |
57 | $res = $rpc->eventloop(); | |
58 | if (!$res) | |
59 | continue; | |
60 | send_sse($res); | |
61 | } | |
62 | } | |
63 | ||
64 | function api_timer_loop(int $every_msec, string $method, array|null $params = null) | |
65 | { | |
66 | GLOBAL $rpc; | |
67 | ||
68 | $rpc->rpc()->add_timer("timer", $every_msec, $method, $params); | |
69 | if ($rpc->error) | |
70 | { | |
69e00a0c BM |
71 | /* Have to resort to old style: client-side timer */ |
72 | while(1) | |
73 | { | |
74 | $res = $rpc->query($method, $params); | |
75 | if (!$res) | |
76 | die; | |
77 | send_sse($res); | |
78 | } | |
fd4848e4 BM |
79 | } |
80 | ||
69e00a0c | 81 | /* New style: use server-side timers */ |
fd4848e4 BM |
82 | for(;;) |
83 | { | |
84 | $res = $rpc->eventloop(); | |
85 | if (!$res) | |
86 | continue; | |
87 | send_sse($res); | |
88 | } | |
89 | } |