hoa / eventsource
Hoa\Eventsource 库。
Requires
- hoa/consistency: ~1.0
- hoa/exception: ~1.0
- hoa/http: ~1.0
Requires (Dev)
- hoa/test: ~2.0
This package is auto-updated.
Last update: 2021-09-20 08:33:44 UTC
README
Hoa 是一套 模块化、可扩展 和 结构化 的 PHP 库。
此外,Hoa 致力于成为工业和学术界之间的桥梁。
Hoa\Eventsource
此库允许通过创建服务器来操作 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。
获取帮助
主要有两种方式获取帮助
- 在
#hoaproject
IRC 频道, - 在论坛上users.hoa-project.net。
贡献
您想贡献力量吗?感谢!详细的贡献指南解释了您需要了解的所有内容。
许可证
Hoa遵循新BSD许可证(BSD-3-Clause)。请参阅LICENSE
获取详细信息。