hoa/eventsource

此包已废弃且不再维护。未建议替代包。

Hoa\Eventsource 库。

3.17.01.10 2017-01-10 10:47 UTC

README

Hoa

Build status Code coverage Packagist License

Hoa 是一套 模块化可扩展结构化 的 PHP 库。
此外,Hoa 致力于成为工业和学术界之间的桥梁。

Hoa\Eventsource

Help on IRC Help on Gitter Documentation Board

此库允许通过创建服务器来操作 EventSource(也称为服务器端事件)技术。

了解更多.

安装

使用 Composer,要将此库包含到依赖中,您需要要求 hoa/eventsource

$ composer require hoa/eventsource '~3.0'

有关更多安装说明,请参阅源代码页面

测试

在运行测试套件之前,必须安装开发依赖项

$ composer install

然后,要运行所有测试套件

$ vendor/bin/hoa test:run

有关更多信息,请参阅贡献者指南

快速使用

我们将提出一个快速概述,从服务器发送无限数量的事件到客户端。客户端将显示所有接收到的事件。因此,在 Server.php

$server = new Hoa\Eventsource\Server();

while (true) {
    // “tick” is the event name.
    $server->tick->send(time());
    sleep(1);
}

然后在 index.html 中,我们的客户端

<pre id="output"></pre>
<script>
var output = document.getElementById('output');

try {
    var source    = new EventSource('Server.php');
    source.onopen = function () {
        output.appendChild(document.createElement('hr'));

        return;
    };
    source.addEventListener('tick', function (evt) {
        var samp       = document.createElement('samp');
        samp.innerHTML = evt.data + '\n';
        output.appendChild(samp);

        return;
    });
} catch (e) {
    console.log(e);
}
</script>

启动您的 HTTP 服务器,然后打开 index.html

Hoa\Eventsource\Server::setReconnectionTime 方法允许重新定义客户端在断开连接后重新连接之前的时间。 Hoa\Eventsource\Server::getLastId 方法允许检索发送到客户端的最后 ID。

Awecode

以下 awecodes 展示了此库的实际应用

  • Hoa\Eventsource: 为什么以及如何使用 Hoa\Eventsource\Server?一个简单且实用的示例将说明 EventSource 技术(或服务器端事件)

文档

《Hoa\Eventsource》黑客手册包含了有关如何使用此库及其工作原理的详细信息。

要本地生成文档,请执行以下命令

$ composer require --dev hoa/devtools
$ vendor/bin/hoa devtools:documentation --open

更多文档可以在项目的网站上找到: hoa-project.net

获取帮助

主要有两种方式获取帮助

贡献

您想贡献力量吗?感谢!详细的贡献指南解释了您需要了解的所有内容。

许可证

Hoa遵循新BSD许可证(BSD-3-Clause)。请参阅LICENSE获取详细信息。