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/stream.php

41 lines
961 B
PHP
Raw Normal View History

2022-03-23 09:08:36 +00:00
<?php $app = require '../../app/inc.php';
2022-03-23 13:03:33 +00:00
use App\Teamtable\TeamMapper;
2022-03-23 09:08:36 +00:00
use App\Timetable\TimeMapper;
2022-03-27 22:19:01 +00:00
use App\SSE\EventLoop;
use App\SSE\StopEventLoopException;
2022-03-23 09:08:36 +00:00
2022-03-23 13:03:33 +00:00
$teamMapper = new TimeMapper($app->database->conn);
2022-03-23 09:08:36 +00:00
$timeMapper = new TimeMapper($app->database->conn);
/**
2022-03-27 22:19:01 +00:00
* Send events to client with Server-Sent Events(SSE)
2022-03-23 09:08:36 +00:00
*/
2022-03-27 22:19:01 +00:00
$sse = new EventLoop();
$sse->interval = 1;
2022-03-23 09:08:36 +00:00
2022-03-27 22:19:01 +00:00
$persist_obj = new class {
public ?int $prev_last_insert = NULL;
};
2022-03-23 09:08:36 +00:00
2022-03-27 22:19:01 +00:00
$sse->start(
function () use ($timeMapper, $teamMapper, &$persist_obj) {
2022-03-23 09:08:36 +00:00
2022-03-27 22:19:01 +00:00
$time = $timeMapper->getLatest();
if ($time)
2022-03-23 09:08:36 +00:00
{
2022-03-27 22:19:01 +00:00
$last_insert = $time->date->getTimestamp();
if ( $persist_obj->prev_last_insert == NULL
|| $last_insert > $persist_obj->prev_last_insert
) {
$persist_obj->prev_last_insert = $last_insert;
2022-03-23 13:03:33 +00:00
2022-03-27 22:19:01 +00:00
return($timeMapper->getAll());
}
2022-03-23 09:08:36 +00:00
}
2022-03-27 22:19:01 +00:00
return;
}
);