From 88fa9960696c388b1077e0853d7c84b3ee6a0daa Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Mon, 7 Sep 2020 10:04:26 +0000 Subject: [PATCH] sse : if there is multiples lines in the log, manage them without loose git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@6086 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- sse.php | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/sse.php b/sse.php index da659d7..1981e36 100644 --- a/sse.php +++ b/sse.php @@ -197,29 +197,33 @@ class sse // Try to get data from backends foreach ($this->files as $event => $filePath) { - $data = $this->backendGet ($event); - if ($data === false) + $lines = $this->backendGet ($event); + if ($lines === false) continue; - $data = rtrim ($data); - if (key_exists ($event, $this->handlers) && - is_callable ($this->handlers[$event])) + $lines = rtrim ($lines); + // As data may contains multiple lines, split them + foreach (explode ("\n", $lines) as $line) { - $array = array ($data); - if (key_exists ($event, $this->handlersParams)) - $array = array_merge ($array, $this->handlersParams[$event]); - $data = call_user_func_array ($this->handlers[$event], $array); + if (key_exists ($event, $this->handlers) && + is_callable ($this->handlers[$event])) + { + $array = array ($line); + if (key_exists ($event, $this->handlersParams)) + $array = array_merge ($array, $this->handlersParams[$event]); + $line = call_user_func_array ($this->handlers[$event], $array); + } + if ($line === false) + continue; + // If the event is data, do not display "event:data", but data: + // immediately + if ($event !== "data") + { + echo "event: $event\n"; + $event = "data"; + } + echo "$event: ".implode ("\n$event: ", explode ("\n", rtrim ($line))). + "\n\n"; } - if ($data === false) - continue; - // If the event is data, do not display "event:data", but data: - // immediately - if ($event !== "data") - { - echo "event: $event\n"; - $event = "data"; - } - echo "$event: ".implode ("\n$event: ", explode ("\n", rtrim ($data))). - "\n\n"; } if (defined ("PHPUNIT")) {