EventSource:总是出错 [英] EventSource: always getting error

查看:930
本文介绍了EventSource:总是出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要开始使用EventSource API,我写了一个学术性最强的示例. 问题是我总是出错并且找不到任何有用的信息.当我加载home.html时,JS脚本在source.onerror处停止;我将其打印到控制台中,但是分析该对象时找不到任何错误类型或消息,因此我不知道这是什么错误.代码中有错误吗?可能是与服务器有关的错误吗?

To get start with EventSource API I wrote the most scholastic example. The problem is that I'm always getting error and I can't find any useful information about it. When I load home.html, the JS script stops at source.onerror; I print it into the console but analyzing the object I can't find any error type or message so I have no idea of what it's wrong. Is there an error in the code? Could be an error related to the server?

home.html

<body>
  <div id="result"></div>
  <script src="sse.js"></script>
</body>

sse.js

function isSseEnabled() {
    return (typeof EventSource !== "undefined");
}

if (isSseEnabled()) {
    var source = new EventSource('source.php');
    source.onerror = function(e) {
        console.log(e);
        source.close();
    };
    source.onmessage = function (e) {
        console.log(e.data);
        document.getElementById('result').innerHTML += e.data.concat('<br>');
    }
} else {
    throw 'SSE not enabled';
}

source.php

<?php
header('Content-Type: text/event-stream');
header('Cache-control: no-cache');

$time = date('r');
echo "Time: $time";
flush();

推荐答案

(TLDR:您未使用SSE协议-请参见此答案的下半部分.)

(TLDR: you are not using the SSE protocol - see second half of this answer.)

您需要做的第一件事是获取正确的错误消息,或者至少查看您的脚本正在生成.我建议使用curl来测试您的脚本.在命令行中:

The first thing you need to do is get a proper error message, or at least look at your script is producing. I'd suggest using curl to test your script. From the commandline:

curl http://127.0.0.1/source.php

(我假设您的html和source.php都在您的本地计算机上,并且未使用https;如果没有,请调整以上内容.还要进行调整以将路径添加到source.php.)

(I've assumed both your html and source.php are on your local machine, and not using https; adjust the above if not. Also adjust to add the path to source.php.)

如果这行得通,或者您没有可用的卷曲,请查看浏览器中的开发人员工具.看一下与source.php的连接正在发生什么,正在发送什么,正在接收什么.

If that works, or you don't have curl available, look at the developer tools in your browser. Look to see what is happening with the connection to source.php, what is being sent, and what is being received.

但是,肯定有两种方法可以改善PHP脚本.首先为消息添加"data:"前缀,并在每条消息后添加两个LF;这是SSE协议所要求的.

However, there are definitely a couple of ways to improve the PHP script. First prefix the message with "data:" and add the couple of LFs after each message; this is required by the SSE protocol.

第二,使用一个有睡眠的无限循环(永远不会有SSE脚本返回一件事并关闭的任何点-在这种情况下,您最好只使用AJAX).所以看起来像这样:

Second, use an infinite loop with a sleep in it (there is never any point having an SSE script that returns one thing and closes - you might as well just use AJAX, in that case). So it would look like this:

<?php
header('Content-Type: text/event-stream');
header('Cache-control: no-cache');

while(true){
    $time = date('r');
    echo "data:Time: $time\n\n";
    @ob_flush();flush();
    sleep(1);
    }

这篇关于EventSource:总是出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆