AliceClass/php/chat/relay.php

43 lines
972 B
PHP
Executable File

<?php
if (!isset($_GET['user'])) {
http_response_code(400);
exit("Missing user");
}
$user = preg_replace('/[^a-zA-Z0-9_-]/', '', $_GET['user']);
// Verifica se a live está online (arquivo .mpd existe)
$mpdPath = "/var/www/html/stream/{$user}.mpd";
if (!file_exists($mpdPath)) {
http_response_code(404);
exit("Live offline");
}
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";
// Cria o arquivo se ainda não existir
if (!file_exists($tmp_file)) {
file_put_contents($tmp_file, '');
}
$lastMessage = '';
while (true) {
clearstatcache();
$currentMessage = trim(file_get_contents($tmp_file));
if ($currentMessage !== '' && $currentMessage !== $lastMessage) {
echo "data: " . $currentMessage . "\n\n";
ob_flush();
flush();
$lastMessage = $currentMessage;
}
sleep(1);
}
?>