<?php
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
header("Access-Control-Allow-Origin: *");
include_once 'base.php';
$lastEventId = floatval(
isset($_SERVER["HTTP_LAST_EVENT_ID"]) ? $_SERVER["HTTP_LAST_EVENT_ID"] :
isset($_GET["lastEventId"]) ? $_GET["lastEventId"] : $_GET["lastMessageID"]
);
echo ":" . str_repeat(" ", 2048) . "\n";
echo "retry: 2000\n\n";
while (true) {
//base('INSERT INTO `chat` SET `name` = '.ex($name).', `text` = '.ex($text).', `ip` = '.ex($ip).', `time` = "'.time().'"');
$result = base('SELECT `id`, `name`, `text`, `time` FROM `chat` WHERE `id` > ' . ex($lastEventId));
if (empty($result)) {
echo "event: ping\n";
echo "data: ping\n\n";
} else {
foreach ($result as $c) {
echo "id: {$c['id']}\n";
echo "data: ";
echo '<div class="menu">';
echo '<img src="img/king.png" width="16" height="16"> '.$c['name'].' ('.vremja($c['time']).')<br><br>'.$c['text'].'<br></div>';
echo "\n\n";
}
$lastEventId = end($result)['id'];
}
ob_flush();
flush();
sleep(1);
}