irap / sse
一个易于使用的、面向对象的用于服务器端发送事件的库
2.2.1
2023-09-26 16:19 UTC
Requires
- php: >=5.3.9
- symfony/http-foundation: ~2.7 | ~3.0 | ~4.0
Requires (Dev)
- phpunit/phpunit: ~4.2 | ~5.0
Suggests
- predis/predis: For using RedisMechnism
- symfony/polyfill-apcu: For APCMecnism on PHP < 7.0
This package is not auto-updated.
Last update: 2024-09-25 19:30:22 UTC
README
一个易于使用的、面向对象的用于服务器端发送事件的库
安装
要安装此软件包,您需要 Composer。
运行 composer require tonyhhyip/sse
用法
服务器端(PHP)
<?php require_once('/path/to/vendor/autoload.php'); //Load with ClassLoader use Sse\Event; use Sse\SSE; //create the event handler class YourEventHandler implements Event { public function update(){ //Here's the place to send data return 'Hello, world!'; } public function check(){ //Here's the place to check when the data needs update return true; } } $sse = new SSE(); //create a libSSE instance $sse->addEventListener('event_name', new YourEventHandler());//register your event handler $sse->start();//start the event loop ?>
客户端(javascript)
var sse = new EventSource('path/to/your/sse/script.php'); sse.addEventListener('event_name',function(e){ var data = e.data; //handle your data here },false);
设置
在您创建了 libSSE 实例之后,有一些设置供您控制其行为。以下是由库提供的设置。
<?php require_once('/path/to/vendor/autoload.php'); //Load with ClassLoader use Sse\SSE; $sse = new SSE(); $sse->set($property, $value);
使用魔术方法保持对属性的直接访问以实现向后兼容。
<?php require_once('/path/to/vendor/autoload.php'); //Load with ClassLoader use Sse\SSE; $sse = new SSE(); $sse->exec_limit = 10; //the execution time of the loop in seconds. Default: 600. Set to 0 to allow the script to run as long as possible. $sse->sleep_time = 1; //The time to sleep after the data has been sent in seconds. Default: 0.5. $sse->client_reconnect = 10; //the time for the client to reconnect after the connection has lost in seconds. Default: 1. $sse->use_chunked_encoding = true; //Use chunked encoding. Some server may get problems with this and it defaults to false $sse->keep_alive_time = 600; //The interval of sending a signal to keep the connection alive. Default: 300 seconds. $sse->allow_cors = true; //Allow cross-domain access? Default: false. If you want others to access this must set to true. ?>
更新
- 添加对 Symfony Http Foundation 组件的支持
- SSE 使用魔术方法而不是直接访问
- 添加 Redis 和 Memcahce 机制
- 添加类似 Session 的机制
- 修复了运行时删除处理程序可能导致状态损坏的事件循环处理问题。
- 使用 Symfony HttpFoundation StreamedResponse 替换旧版本
- 添加更改日志和贡献指南。
针对 PHP 5.3 和 5.4 的特别说明
如果您看到类似 your PHP version does not satisfy that requirement.
的错误消息,请删除 composer.lock 并重新安装。
文档
您可能在这里找到它。 https://github.com/licson0729/libSSE-php/wiki/libSSE-docs
开发
这是一个活跃的项目。如果您想帮助我,请向我提出建议,跟踪问题或找到错误。如果您喜欢它,请考虑给它加星标,以便更多的人知道。
兼容性
由于服务器端事件是一个新标准,并且仍在变化中,因此只有某些浏览器支持它。但是,提供了服务器端事件的 polyfill。此外,在共享主机上,它可能会禁用 PHP 的 set_time_limit
函数,并且库可能无法按预期工作。库中有一些设置可以修复它。
与框架的集成
Symfony
<?php use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sse\SSE; class DefaultController extends Controller { /** * @Route("/sse", name="sse") */ public function sseAction() { $sse = new SSE(); // Add your event listener return $sse->createResponse(); } }
Laravel
请使用 laravel-sse。
Yii2
请使用 yii2-sse。
贡献
请参阅 CONTRIBUTING.md。