HTML5服务器发送事件原型 - 明确的错误和反复投票? [英] HTML5 Server-Sent Events prototyping - ambiguous error and repeated polling?

查看:259
本文介绍了HTML5服务器发送事件原型 - 明确的错误和反复投票?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想它们符合我的要求完美,看起来他们应该是简单的实现去与服务器端事件交手,但是我不能让过去一个模糊的错误,什么样子的连接被反复关闭并重新打开。我已经试过各种基于这个和其他教程。

I'm trying to get to grips with Server-Side Events as they fit my requirements perfectly and seem like they should be simple to implement, however I can't get past a vague error and what looks like the connection repeatedly being closed and re-opened. Everything I have tried is based on this and other tutorials.

在PHP是一个脚本:

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

function sendMsg($id, $msg) {
  echo "id: $id" . PHP_EOL;
  echo "data: $msg" . PHP_EOL;
  echo PHP_EOL;
  ob_flush();
  flush();
}

$serverTime = time();
sendMsg($serverTime, 'server time: ' . date("h:i:s", time()));
?>

和JavaScript看起来像这样(在身体负荷运行):

and the JavaScript looks like this (run on body load):

function init() {

    var source;
    if (!!window.EventSource) {
        source = new EventSource('events.php');
        source.addEventListener('message', function(e) {
            document.getElementById('output').innerHTML += e.data + '<br />';
        }, false);
        source.addEventListener('open', function(e) {
            document.getElementById('output').innerHTML += 'connection opened<br />';
        }, false);
        source.addEventListener('error', function(e) {
            document.getElementById('output').innerHTML += 'error<br />';
        }, false);
    }
    else {
        alert("Browser doesn't support Server-Sent Events");
    }
}

我已经搜索了一下周围,但无法找到的信息

I have searched around a bit but can't find information on


  1. 如果Apache需要任何特殊的配置,以支持服务器发送的事件和

  2. 我怎么能启动从服务器推用这种设置的(例如我可以简单地执行从CLI PHP脚本给推到已经连接的浏览器?)

如果我运行这个JS在Chrome(16.0.912.77),它打开连接,接收时间,那么错误(在错误对象中没有有用的信息),然后重新连接在3秒钟内,通过同样的过程进行。在Firefox(10.0),我得到了相同的行为。

If I run this JS in Chrome (16.0.912.77) it opens the connection, receives the time, then errors (with no useful information in the error object), then reconnects in 3 seconds and goes through the same process. In Firefox (10.0) I get the same behaviour.

修改1 :我想这个问题可能与我所用的服务器,所以我在香草测试XAMPP安装和同样的错误出现。如果一个基本的服务器配置能够处理这种不加修饰/额外的配置?

EDIT 1: I thought the issue could be related to the server I was using, so I tested on a vanilla XAMPP install and the same error comes up. Should a basic server configuration be able to handle this without modification / extra configuration?

编辑2 :下面是从浏览器输出的示例:

EDIT 2: The following is an example of output from the browser:

connection opened
server time: 01:47:20
error
connection opened
server time: 01:47:23
error
connection opened
server time: 01:47:26
error

谁能告诉我这是怎么回事了?我所看到的教程,使它看起来像SSE是非常简单的。另外要我的两个编号的问题的任何答案上面会非常有帮助。

Can anyone tell me where this is going wrong? The tutorials I have seen make it look like SSE is very straightforward. Also any answers to my two numbered questions above would be really helpful.

感谢。

推荐答案

问题是你的PHP。

使用PHP脚本编写方式,只有一个消息是每次执行发送。那是,如果你直接访问PHP文件它是如何工作的,这就是,如果你用的EventSource访问文件,它是如何工作。因此,为了使你的PHP脚本发送多条消息,你需要一个循环。

With the way your php script is written, only one message is sent per execution. That's how it works if you access the php file directly, and that's how it works if you access the file with an EventSource. So in order to make your php script send multiple messages, you need a loop.

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

function sendMsg($id, $msg) {
  echo "id: $id" . PHP_EOL;
  echo "data: $msg" . PHP_EOL;
  echo PHP_EOL;
  ob_flush();
  flush();
}
while(true) {
  $serverTime = time();
  sendMsg($serverTime, 'server time: ' . date("h:i:s", time()));
  sleep(1);
}
?>

我改变了你的code,包括一个无限循环发送每封邮件(下面的例子在这里找到后,等待1秒:的使用服务器发送的事件的)。

这类型的循环是我目前使用的,它消除了持续连接下降并重新每3秒。但是,(我只在Chrome测试这一点),连接现在只有30秒保持活。我会继续弄清楚为什么是这样的话,当我找到一个我会后一个解决方案,但在那之前至少应该让你更接近你的目标。

This type of loop is what I'm currently using and it eliminated the constant connection drop and reconnect every 3 seconds. However (and I've only tested this in chrome), the connections are now only kept alive for 30 seconds. I will be continuing to figure out why this is the case and I'll post a solution when I find one, but until then this should at least get you closer to your goal.

希望帮助,

编辑:

为了保持连接打开可笑长时间使用PHP,你需要设置的max_execution_time(感谢tomfumb此)。这可以在至少三种方式完成:

In order to keep the connection open for ridiculously long times with php, you need to set the max_execution_time (Thanks to tomfumb for this). This can be accomplished in at least three ways:


  1. 如果你可以改变你的php.ini,改变价值的max_execution_time。这将允许所有脚本为你虽然指定时间运行。

  2. 在你想长时间运行,使用功能的ini_set(键,值)的脚本,其中关键是的max_execution_time和值是在几秒钟内你希望你的脚本来运行的时间。

  3. 在你想长时间运行,使用功能或者set_time_limit(n),其中n是你希望你的脚本运行的秒数脚本。

这篇关于HTML5服务器发送事件原型 - 明确的错误和反复投票?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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