AliceClass/php/chat/relay.php

36 lines
733 B
PHP

<?php
if (!isset($_GET['user'])) {
http_response_code(400);
exit("Missing user");
}
$user = preg_replace('/[^a-zA-Z0-9_-]/', '', $_GET['user']);
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
header("Connection: keep-alive");
$tmp_file = sys_get_temp_dir() . "/relay_chat_{$user}.txt";
if (!file_exists($tmp_file)) {
file_put_contents($tmp_file, '');
}
$last_modif = 0;
while (true) {
clearstatcache();
$current_modif = filemtime($tmp_file);
if ($current_modif > $last_modif) {
$last_modif = $current_modif;
$data = file_get_contents($tmp_file);
echo "data: " . trim($data) . "\n\n";
ob_flush();
flush();
}
sleep(1);
}
?>