-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSE.php
More file actions
50 lines (43 loc) · 1.23 KB
/
SSE.php
File metadata and controls
50 lines (43 loc) · 1.23 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
<?php namespace ProcessWire;
class SSE {
public static function header() {
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('Cache-Control: no-store');
header('Pragma: no-cache');
header('Expires: 0');
header('Connection: keep-alive');
if (ob_get_level() > 0) {
ob_end_flush();
}
ini_set('output_buffering', '0');
ini_set('zlib.output_compression', '0');
set_time_limit(0);
ini_set('max_execution_time', 0);
if (ob_get_level() == 0) {
ob_start();
}
}
public static function send(string $data, string $event = 'message', $addKeepAlive = false) {
echo "event: {$event}\n";
echo "data: ".json_encode($data)."\n\n";
if ($addKeepAlive) {
echo ": keep-alive\n\n";
}
echo str_pad('', 8196)."\n";
ob_flush();
flush();
}
public static function ping() {
echo ": ping\n\n";
ob_flush();
flush();
}
public static function close() {
echo "event: close\n";
echo "data:\n\n";
echo str_pad('', 8196)."\n";
ob_flush();
flush();
}
}