This repository has been archived on 2023-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
web/public/race/long-polling.php
2022-03-21 12:28:18 +01:00

39 lines
1.0 KiB
PHP

<?php $app = require '../../app/inc.php';
use App\Timetable\TimeMapper;
$timeMapper = new TimeMapper($app->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;
}