From ecded9573d71d85605c2b29dd30edc86c431e4e9 Mon Sep 17 00:00:00 2001 From: William Date: Mon, 21 Mar 2022 12:28:18 +0100 Subject: [PATCH] Commit --- app/view/pages/race/live-results.php | 30 ++++++++++++++++++++++ app/view/template/header.php | 1 + public/race/live-results.php | 5 ++++ public/race/long-polling.php | 38 ++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 app/view/pages/race/live-results.php create mode 100644 public/race/live-results.php create mode 100644 public/race/long-polling.php diff --git a/app/view/pages/race/live-results.php b/app/view/pages/race/live-results.php new file mode 100644 index 0000000..52ebbb7 --- /dev/null +++ b/app/view/pages/race/live-results.php @@ -0,0 +1,30 @@ +

Live resultater

+

Venter på data...

+ + \ No newline at end of file diff --git a/app/view/template/header.php b/app/view/template/header.php index 0f36deb..83f24b8 100644 --- a/app/view/template/header.php +++ b/app/view/template/header.php @@ -38,6 +38,7 @@

Felles

user->loggedIn): ?> diff --git a/public/race/live-results.php b/public/race/live-results.php new file mode 100644 index 0000000..3390160 --- /dev/null +++ b/public/race/live-results.php @@ -0,0 +1,5 @@ +view('template/header', ["title" => "Live resultater"]); +$app->view('pages/race/live-results'); +$app->view('template/footer'); \ No newline at end of file diff --git a/public/race/long-polling.php b/public/race/long-polling.php new file mode 100644 index 0000000..3b72f4f --- /dev/null +++ b/public/race/long-polling.php @@ -0,0 +1,38 @@ +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; +} +