gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:31:58 26 4
gpt4 key购买 nike

I have a php script that import large data from csv files with validations.
For that I need to show progress to the user. I have used Event Streaming for that.
When I echo something, I want it to be transferred to client one by one instead of server sent whole output in bulk.
I had already played around with ob_start(), ob_implicit_flush() & ob_flush(), but they didn't work.
My script is working perfect on another server. Below server configurations are given:

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

OS: LinuxPHP Version 5.4.36-0+deb7u3Server API: CGI/FastCGI Memory_limit: 128Moutput_buffering: no value

As I have said, the code is working properly on another server which has the almost same configuration, i.e.

OS: LinuxPHP Version 5.4.37Server API: CGI/FastCGI Memory_limit: 256MBoutput_buffering: no value

Below is my sample code for sending event:

<?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>

最佳答案

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

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

一个很好的网络套接字教程 http://www.phpbuilder.com/articles/application-architecture/optimization/creating-real-time-applications-with-php-and-websockets.html

使用这种方法,如果您想要实现验证,那么服务器不仅会发送数据 block ,还会根据 javascript 的请求发送数据 block

所以你的客户可以说我需要 block 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/

关于javascript - 事件源 -> 服务器批量返回事件流而不是 block 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28607708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com