update chat
Deploy / Deploy (push) Successful in 14s

This commit is contained in:
www-data committed 2025-03-27 11:53:08 -03:00
1 parent 756d2a98b2
commit a00dcc4e70
4 files changed
+144 -70

No files matched your search

Regular → Executable
+14 -7
View File
@@ -5,31 +5,38 @@ if (!isset($_GET['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, '');
}
$last_modif = 0;
$lastMessage = '';
while (true) {
clearstatcache();
$current_modif = filemtime($tmp_file);
$currentMessage = trim(file_get_contents($tmp_file));
if ($current_modif > $last_modif) {
$last_modif = $current_modif;
$data = file_get_contents($tmp_file);
echo "data: " . trim($data) . "\n\n";
if ($currentMessage !== '' && $currentMessage !== $lastMessage) {
echo "data: " . $currentMessage . "\n\n";
ob_flush();
flush();
$lastMessage = $currentMessage;
}
sleep(1);
}
?>