garlic/event-source

此包的最新版本(4.0.3)没有可用的许可信息。

SSE 的 Eventsource 库。

安装次数: 1,308

依赖关系: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 24

类型:symfony-bundle

4.0.3 2017-09-07 11:36 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 在 New BSD 许可证(BSD-3-Clause)下。有关详细信息,请参阅 LICENSE