-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsseprep.php
46 lines (38 loc) · 1.08 KB
/
sseprep.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
37
38
39
40
41
42
43
44
45
46
<?php
set_time_limit(0);
header("Content-type:text/event-stream");
header("Cache-Content: no-cache");
require_once("config.php");
ob_start();
$query1 = "SELECT * from posts";
$res1 = $db->query($query1);
$old_row_count = $res1->num_rows;
while(true){
$res2 = $db->query($query1);
$new_row_count = $res2->num_rows;
if($new_row_count > $old_row_count){
$new_posts_count = $new_row_count - $old_row_count;
# get all the "new" posts from the database;
$query2 = "SELECT * FROM posts ORDER BY id DESC LIMIT ".$new_posts_count;
$res3 = $db->query($query2);
$data = array();
while($row = $res3->fetch_assoc()){
$post = array(
"username"=>$row['username'],
"title"=>$row['title'],
"content"=>$row['content'],
"id"=>$row['id']
);
array_push($data, $post);
}
echo "event:received\n";
echo "retry:10000\n";
echo "data: ".json_encode($data)."\n\n";
//echo "data: ".json_encode($data)."\n\n";
ob_flush();
flush();
$old_row_count = $new_row_count;
}
sleep(3);
}
?>