所以我正在使用SSE EventListener,此脚本将侦听在response.php上发生的OnMessage事件
if(typeof(EventSource !== "undefined")){
var source = new EventSource('respond.php');
source.addEventListener('message', function(e) {
console.log(e.data);
}, false);}
else{
console.log("Sorry SSE is not working");
}
Respond.php
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
session_start();
if(isset($_POST['Body'])){
$myfile = fopen("data.txt","w");
$input = $_POST['Body'];
fwrite($myfile,"$input");
fclose($myfile);
echo "data: $input\n\n";
ob_flush();
flush();
}
这个respond.php代码基本上说当这个php获得一个POST变量准备就绪然后将它写入data.txt然后回显数据。每当我发布$ _POST [&#39; Body&#39;。 data.txt得到更新,这意味着IF内部正在执行但是,回声部分刚刚被解雇? JAVASCRIPT onMessageListener只是没有把它拿起来?(我确实尝试创建一个ELSE Part并回显一些东西,而Listener选择了它。)所以我真的不知道这里发生了什么。有人可以帮忙吗?