noisim / ss-event
这个库为Laravel提供了服务器发送事件。
dev-master
2017-10-20 10:59 UTC
Requires
- illuminate/support: ^5.5
- tonyhhyip/sse: ^2.1
This package is auto-updated.
Last update: 2024-09-27 23:51:20 UTC
README
这个库为Laravel提供了服务器发送事件。已在Laravel 5.5上测试。
如何使用
此软件包通过Composer安装。要安装,只需将其添加到您的composer.json文件
{
"require": {
"noisim/ss-event": "dev-master"
}
}
然后运行Composer以更新依赖项 composer update。
然后打开您的Laravel配置文件config/app.php,并在$providers数组中添加此软件包的服务提供者。
\Noisim\SSEvent\SSEventServiceProvider::class
最后,在控制台运行以生成配置文件
php artisan vendor:publish --tag=config
Laravel控制器
<?php namespace App\Http\Controllers; use Noisim\SSEvent\SSEvent; class TestController extends Controller { public function test() { $sse = new SSEvent(); return $sse->sleepTime(10)->addEvent("event_name", function () { return json_encode(["hello" => "world"]); })->start(); } }
Laravel路由
<?php Route::get('test', 'TestController@test')->name("test");
客户端JavaScript
var source = new EventSource('/test'); source.addEventListener('event_name', function(event) { console.log(event); }, false);