Commit
This commit is contained in:
		
							parent
							
								
									2f19e5eb4e
								
							
						
					
					
						commit
						aebd249237
					
				@ -72,7 +72,7 @@ class App
 | 
				
			|||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Turn data array into json response
 | 
					     * Turn data array into json response
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public function api(array $data, int $status_code = 200): void
 | 
					    public function api($data, int $status_code = 200): void
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        // set headers
 | 
					        // set headers
 | 
				
			||||||
        http_response_code($status_code);
 | 
					        http_response_code($status_code);
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,6 @@
 | 
				
			|||||||
<h1>Live resultater</h1>
 | 
					<h1>Live resultater</h1>
 | 
				
			||||||
<br>
 | 
					<br>
 | 
				
			||||||
<table>
 | 
					<table id ="table">
 | 
				
			||||||
    <tr>
 | 
					    <tr>
 | 
				
			||||||
        <th>Some shit</th>
 | 
					        <th>Some shit</th>
 | 
				
			||||||
        <th>Some shit</th>
 | 
					        <th>Some shit</th>
 | 
				
			||||||
@ -8,9 +8,24 @@
 | 
				
			|||||||
</table>
 | 
					</table>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<script>
 | 
					<script>
 | 
				
			||||||
var es = new EventSource('stream.php');
 | 
					hash = 0;
 | 
				
			||||||
es.onmessage = function(event)
 | 
					async function doTheThang()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    console.log("New message", event.data);
 | 
					    response = await fetch("sync.php?hash=" + hash);
 | 
				
			||||||
};
 | 
					
 | 
				
			||||||
 | 
					    if (response.status === 204)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    json = await response.json();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    hash = json.hash;
 | 
				
			||||||
 | 
					    data = json.data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    document.getElementById("table").innerHTML = json.data;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					setInterval(function() { doTheThang() }, 1000)
 | 
				
			||||||
 | 
					doTheThang();
 | 
				
			||||||
</script>
 | 
					</script>
 | 
				
			||||||
@ -1,12 +1,14 @@
 | 
				
			|||||||
<?php $app = require '../../app/inc.php';
 | 
					<?php $app = require '../../app/inc.php';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// uh oh.. stinky
 | 
				
			||||||
 | 
					
 | 
				
			||||||
$cardreader = $app->model('Cardreader');
 | 
					$cardreader = $app->model('Cardreader');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if ($_SERVER['REQUEST_METHOD'] === 'POST')
 | 
					if ($_SERVER['REQUEST_METHOD'] === 'POST')
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    if (isset($_POST['cardnumber']) && strlen($_POST['cardnumber']) !== 0)
 | 
					    if (isset($_POST['cardnumber']) && strlen((string)$_POST['cardnumber']) !== 0)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $cardnumber = $_POST['cardnumber'];
 | 
					        $cardnumber = (string)$_POST['cardnumber'];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            $code = $cardreader->receive($cardnumber, 5);
 | 
					            $code = $cardreader->receive($cardnumber, 5);
 | 
				
			||||||
 | 
				
			|||||||
@ -1,39 +0,0 @@
 | 
				
			|||||||
<?php $app = require '../../app/inc.php';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
use App\Teamtable\TeamMapper;
 | 
					 | 
				
			||||||
use App\Timetable\TimeMapper;
 | 
					 | 
				
			||||||
use App\SSE\EventLoop;
 | 
					 | 
				
			||||||
use App\SSE\StopEventLoopException;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
$teamMapper = new TimeMapper($app->database->conn);
 | 
					 | 
				
			||||||
$timeMapper = new TimeMapper($app->database->conn);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/**
 | 
					 | 
				
			||||||
 * Send events to client with Server-Sent Events(SSE)
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
$sse = new EventLoop();
 | 
					 | 
				
			||||||
$sse->interval = 1; 
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
$persist_obj = new class {
 | 
					 | 
				
			||||||
        public ?int $prev_last_insert = NULL;
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
$sse->start(
 | 
					 | 
				
			||||||
    function () use ($timeMapper, $teamMapper, &$persist_obj) {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $time = $timeMapper->getLatest();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if ($time)
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            $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;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                return($timeMapper->getAll());
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
);
 | 
					 | 
				
			||||||
							
								
								
									
										27
									
								
								public/race/sync.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								public/race/sync.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
				
			|||||||
 | 
					<?php $app = require '../../app/inc.php';
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * We originally wanted to use SSE for this, but the hosting provider
 | 
				
			||||||
 | 
					 * did not support that so we resorted to simple polling instead
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use App\Teamtable\TeamMapper;
 | 
				
			||||||
 | 
					use App\Timetable\TimeMapper;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					$teamMapper = new TimeMapper($app->database->conn);
 | 
				
			||||||
 | 
					$timeMapper = new TimeMapper($app->database->conn);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					$prev_hash = (int)filter_input(INPUT_GET, 'hash');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					$times = $timeMapper->getAll();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					$hash = crc32(serialize($timeMapper->getAll()));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if ($prev_hash !== $hash)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    $app->api([
 | 
				
			||||||
 | 
					            "hash" => $hash,
 | 
				
			||||||
 | 
					            "data" => $times
 | 
				
			||||||
 | 
					        ]);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					// return nothing
 | 
				
			||||||
 | 
					http_response_code(204);
 | 
				
			||||||
		Reference in New Issue
	
	Block a user