事件源 -> 服务器批量返回事件流而不是块返回

2024-01-26

我有一个 php 脚本,可以通过验证从 csv 文件导入大数据。
为此,我需要向用户展示进度。我为此使用了事件流。
当我回显某些内容时,我希望将其一一传输到客户端,而不是服务器批量发送整个输出。
我已经尝试过 ob_start()、ob_implicit_flush() 和 ob_flush(),但它们不起作用。
我的脚本在另一台服务器上运行完美。 下面给出了服务器配置:

代码未按预期响应的服务器配置,即


OS: Linux
PHP Version 5.4.36-0+deb7u3
Server API: CGI/FastCGI 
Memory_limit: 128M
output_buffering: no value
  

正如我所说,代码在另一台具有几乎相同配置的服务器上正常工作,即


OS: Linux
PHP Version 5.4.37
Server API: CGI/FastCGI 
Memory_limit: 256MB
output_buffering: no value
  

以下是我发送事件的示例代码:

<?php
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
header("Access-Control-Allow-Origin: *");

$lastEventId = floatval(isset($_SERVER["HTTP_LAST_EVENT_ID"]) ? $_SERVER["HTTP_LAST_EVENT_ID"] : 0);
if ($lastEventId == 0) {
    $lastEventId = floatval(isset($_GET["lastEventId"]) ? $_GET["lastEventId"] : 0);
}

echo ":" . str_repeat(" ", 2048) . "\n"; // 2 kB padding for IE
echo "retry: 2000\n";

// event-stream
$i = $lastEventId;

while ($i <= 100) {
    if($i==100){
        echo "data: stop\n";
        ob_flush();
        flush();
        break;
    } else {
        echo "id: " . $i . "\n";
        echo "data: " . $i . ";\n\n";
        ob_flush();
        flush();
        sleep(1);
    }
    $i++;
}
?>

以下是我需要回复的客户页面:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title>EventSource example</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <script src="../jquery/eventsource.js"></script>
    <script>
        var es = new EventSource("events.php");
        var listener = function(event) {
            console.log(event.data);
            var type = event.type;
            if (event.data == 'stop') {
                es.close();
            } else {
                var div = document.createElement("div");
                div.appendChild(document.createTextNode(type + ": " + (type === "message" ? event.data : es.url)));
                document.body.appendChild(div);
            }
        };
        var errlistener = function(event) {
            es.close();
        }
        es.addEventListener("open", listener);
        es.addEventListener("message", listener);
        es.addEventListener("error", errlistener);
    </script>
</head>

<body>
</body>

</html>

将分块数据返回到浏览器的最佳方法是使用 Websockets 让客户端打开文件读取器的套接字,然后您可以毫无问题地将数据分块到浏览器。

一旦完成,您就可以关闭套接字。

a good tutorial for web sockets http://www.phpbuilder.com/articles/application-architecture/optimization/creating-real-time-applications-with-php-and-websockets.html http://www.phpbuilder.com/articles/application-architecture/optimization/creating-real-time-applications-with-php-and-websockets.html

使用此方法,如果您想要实现验证,那么服务器不仅仅是发送块,而是根据 javascript 的请求发送块

所以你的客户端可能会说我需要块 5 并且你的服务器实现类似的东西

$requestedChunk = 5; // this would be set by the javascript sending the request
$chunkSize = 256; // this would be your chunk size;

$readPossition = $requestedChunk * $chunkSize;

链接不再有效,因此这是一个基于 Ratchet 构建的链接:https://blog.samuelattard.com/the-tutorial-for-php-websockets-that-i-wish-had-existed/ https://blog.samuelattard.com/the-tutorial-for-php-websockets-that-i-wish-had-existed/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

事件源 -> 服务器批量返回事件流而不是块返回 的相关文章

随机推荐