The simplest implementation of the chat with a list of users online. One SSE (stream) gives you new lines in the txt-file, and another script with SSE is responsible for the list of users present. And here with it absolutely trouble. If a couple of times to refresh the page in which this script is being listened to, the server freezes for 15 minutes. If it scoffs at it again, it stops responding permanently. It seems that when the page is updated / closed, the script continues to rotate. Or the load is too large ... Please see what could be wrong here.

<?php ignore_user_abort(true); header('Content-Type: text/event-stream'); header("Cache-Control: no-cache"); $hash_old = ""; session_name(' myApp' ); # получаем юзернейм обратившегося session_start(); $userName = $_SESSION['userName']; session_write_close(); mysql_select_db('mydb', $db); # Пока обратившийся держит коннект, пишем каждые 20 секунд # в БД время последней активности и вторым запросом выводим тех, # чьё время последней активности менее 20 секунд назад. while(!connection_status() !== 0){ $query = "UPDATE `user` SET ping = NOW() WHERE name = '".$userName."'"; $result = mysql_query ( $query ); $query = "SELECT name FROM `user` WHERE ping >= DATE_SUB(NOW(), INTERVAL 19 second) ORDER BY ping DESC"; $result = mysql_query ( $query ); $names = []; while($row = mysql_fetch_array($result)) array_push($names, $row['name']); if (count($names) > 0) { $names_str = implode(",", $names); # если хэш строки с именами не равен хэшу предыдущей итерации... # Не спрашивайте, почему я просто не сравниваю строки $hash_new = md5($names_str); if ($hash_new !== $hash_old) { $response = ["users" => $names_str]; $hash_old = $hash_new; # выводим json с именами echo "data: ".json_encode($response)."\n\n"; } } ob_flush(); flush(); sleep(20); # делаем паузу } ?> 
  • one
    !connection_status() is always only true or false . At the same time, neither true nor false is NEVER zero. Here is the eternal cycle - andreymal
  • Well, in general, chat rooms need to be done using WebSocket - andreymal
  • @andreymal thanks, overlooked. But with while (connection_status ()! == -1) is the same in general. - Turambar
  • one
    If I understand correctly the help php, connection_status() never happens -1 - andreymal
  • Thanks for the direction, I will dig. - Turambar

0