diff --git a/app/lib/App/SSE/Sender.php b/app/lib/App/SSE/Sender.php new file mode 100644 index 0000000..706337d --- /dev/null +++ b/app/lib/App/SSE/Sender.php @@ -0,0 +1,35 @@ +Live resultater
Venter på data...
- + \ No newline at end of file diff --git a/public/race/long-polling.php b/public/race/long-polling.php deleted file mode 100644 index 3b72f4f..0000000 --- a/public/race/long-polling.php +++ /dev/null @@ -1,38 +0,0 @@ -database->conn); - -/** - * Long polling - */ -session_write_close(); -ignore_user_abort(false); -set_time_limit(30); - -// if ajax request has send a timestamp, then $last_ajax_call = timestamp, else $last_ajax_call = null -$last_ajax_call = isset($_GET['timestamp']) ? (int)$_GET['timestamp'] : NULL; - -// main loop -while(TRUE) -{ - $time = $timeMapper->getLatest(); - - if ($time) - { - $last_change_in_timetable = $time->date->getTimestamp(); - - if ($time && $last_ajax_call == NULL || $last_change_in_timetable > $last_ajax_call) - { - exit(json_encode(["data" => json_encode($timeMapper->getAll()), "timestamp" => $last_change_in_timetable])); - } - } - - // PHP caches file data, like requesting the size of a file, by default. clearstatcache() clears that cache - clearstatcache(); - // wait for 1 sec (not very sexy as this blocks the PHP/Apache process, but that's how it goes) - sleep(1); - continue; -} - diff --git a/public/race/stream.php b/public/race/stream.php new file mode 100644 index 0000000..b117c73 --- /dev/null +++ b/public/race/stream.php @@ -0,0 +1,30 @@ +database->conn); + +/** + * Server-Sent Events (SSE) + */ + +$sse = new Sender(); + +$last_change_in_timetable; +while (!connection_aborted()) +{ + $time = $timeMapper->getLatest(); + + if ($time) + { + $last_change_in_timetable = $time->date->getTimestamp(); + + if ($last_time == NULL || $last_change_in_timetable > $last_ajax_call) + { + $sse->send($timeMapper->getAll()); + } + } + + sleep(1); +} \ No newline at end of file