一个易于使用、面向对象的PHP库,用于服务器端事件

3.0.0 2021-04-02 13:59 UTC

This package is auto-updated.

Last update: 2024-09-29 05:55:27 UTC


README

License Build Status

一个易于使用、面向对象的用于服务器端事件的库。

此存储库最初是从tonyhhyip/libSSE-php分叉的。增加了一些选项,使其能够在FastCgi无法禁用输出缓存的PHP-FastCGI设置中工作。

如果你正在运行PHP-FastCGI

  • 检查你的fcgid.conf文件中的FcgidOutputBufferSize参数:为了获得最佳效果,它不应大于8192(8K)。
  • 使用content_encoding_none = trueclose_connection = true初始化SSE。你可能需要将pad_response_data设置为等于或略大于FcgidOutputBufferSize的值。

安装

要安装此包,您需要composer

运行composer require frank-lar/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_encodung = 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.
	$sse->content_encoding_none = false; // Disable compression in case content length is compressed (useful with php-fastcgi)
	$sse->close_connection = false; // Send a "Connection: close" header before flush (useful with php-fastcgi)
	$sse->pad_response_data = 0; // Concatenate N "\n" characters to force output buffer flushing
	
	?>

更新

  1. 支持Symfony Http Foundation组件
  2. SSE使用魔术方法而不是直接访问
  3. 添加Redis和Memcahce机制
  4. 添加类似会话的机制
  5. 修复了在运行时删除处理程序可能导致状态损坏的事件循环处理
  6. 使用Symfony HttpFoundation StreamedResponse替换旧版本
  7. 添加更改日志和贡献指南。
  8. 添加适用于某些PHP-FastCgi设置的选项。
  9. 添加使用"\n"字符填充数据以强制输出缓冲区刷新的选项。

特别针对PHP 5.3和5.4

如果你看到类似于你的PHP版本不满足这些要求。的错误消息,请删除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