This commit is contained in:
William 2022-03-21 12:28:18 +01:00
parent 87c1a0c1c5
commit ecded9573d
4 changed files with 74 additions and 0 deletions

View File

@ -0,0 +1,30 @@
<h1>Live resultater</h1>
<p id="response">Venter data...</p>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script>
function getContent(timestamp)
{
var queryString = {'timestamp' : timestamp};
$.ajax(
{
type: 'GET',
url: 'long-polling.php',
data: queryString,
success: function(data){
// put result data into "obj"
var obj = jQuery.parseJSON(data);
// put the data_from_file into #response
$('#response').html(obj.data);
// call the function again, this time with the timestamp we just got from server.php
getContent(obj.timestamp);
}
}
);
}
// initialize jQuery
$(function() {
getContent();
});
</script>

View File

@ -38,6 +38,7 @@
<h4>Felles</h4> <h4>Felles</h4>
<ul> <ul>
<li><a href="<?=$this->config['root_url']?>index.php">Forside</a></li> <li><a href="<?=$this->config['root_url']?>index.php">Forside</a></li>
<li><a href="<?=$this->config['root_url']?>race/live-results.php">Resultater</a></li>
</ul> </ul>
<?php if ($this->user->loggedIn): ?> <?php if ($this->user->loggedIn): ?>

View File

@ -0,0 +1,5 @@
<?php $app = require '../../app/inc.php';
$app->view('template/header', ["title" => "Live resultater"]);
$app->view('pages/race/live-results');
$app->view('template/footer');

View File

@ -0,0 +1,38 @@
<?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;
}