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 that need power level 1 and up
[ [
"routes" => [ "routes" => [
"race/*" "race/simulator.php",
"race/configure/*"
], ],
"catcher" => [ "catcher" => [
"name" => "page", "name" => "page",

View File

@ -9,7 +9,7 @@ namespace App\SSE;
*/ */
class Sender class Sender
{ {
public function __construct() public function start(): void
{ {
header("Cache-Control: no-store"); header("Cache-Control: no-store");
header("Content-Type: text/event-stream"); header("Content-Type: text/event-stream");
@ -17,9 +17,13 @@ class Sender
// as session data is locked to prevent concurrent writes we // as session data is locked to prevent concurrent writes we
// make it read only to prevent the server from locking up // make it read only to prevent the server from locking up
session_write_close(); 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) if ($event)
{ {

View File

@ -1,10 +1,26 @@
<h1>Live resultater</h1> <h1>Live resultater</h1>
<p id="response">Venter data...</p> <br>
<table>
<tr>
<th>Some shit</th>
<th>Some shit</th>
</tr>
</table>
<script> <script>
var source = new EventSource('stream.php'); var source = new EventSource('stream.php');
source.addEventListener('message', function(e) { source.addEventListener('message', function(e)
console.log(e.data); {
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); }, false);
</script> </script>

View File

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