This commit is contained in:
William 2022-03-23 14:03:33 +01:00
parent 63ac3c49e1
commit cf452a7f06
4 changed files with 36 additions and 11 deletions

View File

@ -23,7 +23,8 @@ class AccessControl
// routes that need power level 1 and up
[
"routes" => [
"race/*"
"race/simulator.php",
"race/configure/*"
],
"catcher" => [
"name" => "page",

View File

@ -9,7 +9,7 @@ namespace App\SSE;
*/
class Sender
{
public function __construct()
public function start(): void
{
header("Cache-Control: no-store");
header("Content-Type: text/event-stream");
@ -17,9 +17,13 @@ class Sender
// as session data is locked to prevent concurrent writes we
// make it read only to prevent the server from locking up
session_write_close();
// we have to flush before because idk
ob_end_flush();
flush();
}
public function send($data, $event = NULL): void
public function flush($data, $event = NULL): void
{
if ($event)
{

View File

@ -1,10 +1,26 @@
<h1>Live resultater</h1>
<p id="response">Venter data...</p>
<br>
<table>
<tr>
<th>Some shit</th>
<th>Some shit</th>
</tr>
</table>
<script>
var source = new EventSource('stream.php');
source.addEventListener('message', function(e) {
console.log(e.data);
source.addEventListener('message', function(e)
{
rows = JSON.parse(e.data);
console.log(rows);
// update table
root = document.getElementById("root");
rows.forEach(row => root.insertAdjacentHTML(
'beforebegin',
`<tr><td>${row.teamId}</td><td>${row.id}</td></tr>`
));
}, false);
</script>

View File

@ -1,28 +1,32 @@
<?php $app = require '../../app/inc.php';
use App\Teamtable\TeamMapper;
use App\Timetable\TimeMapper;
use App\SSE\Sender;
$teamMapper = new TimeMapper($app->database->conn);
$timeMapper = new TimeMapper($app->database->conn);
/**
* Server-Sent Events (SSE)
*/
$sse = new Sender();
$sse->start();
$last_change_in_timetable;
$prev_last_insert = NULL;
while (!connection_aborted())
{
$time = $timeMapper->getLatest();
if ($time)
{
$last_change_in_timetable = $time->date->getTimestamp();
$last_insert = $time->date->getTimestamp();
if ($last_time == NULL || $last_change_in_timetable > $last_ajax_call)
if ($prev_last_insert == NULL || $last_insert > $prev_last_insert)
{
$sse->send($timeMapper->getAll());
$sse->flush($timeMapper->getAll());
$prev_last_insert = $last_insert;
}
}