Commit
This commit is contained in:
parent
87c1a0c1c5
commit
ecded9573d
30
app/view/pages/race/live-results.php
Normal file
30
app/view/pages/race/live-results.php
Normal file
@ -0,0 +1,30 @@
|
||||
<h1>Live resultater</h1>
|
||||
<p id="response">Venter på 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>
|
@ -38,6 +38,7 @@
|
||||
<h4>Felles</h4>
|
||||
<ul>
|
||||
<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>
|
||||
|
||||
<?php if ($this->user->loggedIn): ?>
|
||||
|
5
public/race/live-results.php
Normal file
5
public/race/live-results.php
Normal 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');
|
38
public/race/long-polling.php
Normal file
38
public/race/long-polling.php
Normal 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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user