Skip to content

Commit 0a39022

Browse files
committed
fix(test): event 事件使用用 fire 方法
```php use Hhxsv5\LaravelS\Swoole\Task\Event; Event::fire(new TaskEvent('event data')); ```
1 parent 5f2f44c commit 0a39022

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

laravel/app/Events/TestEvent.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* 创建事件类--异步事件
4+
* 此特性依赖Swoole的AsyncTask,必须先设置config/laravels.php的swoole.task_worker_num。
5+
* 异步事件的处理能力受Task进程数影响,需合理设置task_worker_num。
6+
* User: lisgroup
7+
* Date: 19-4-13
8+
* Time: 下午10:23
9+
*/
10+
11+
namespace App\Events;
12+
13+
use Hhxsv5\LaravelS\Swoole\Task\Event;
14+
15+
class TestEvent extends Event
16+
{
17+
private $data;
18+
19+
public function __construct($data)
20+
{
21+
$this->data = $data;
22+
}
23+
24+
public function getData()
25+
{
26+
return $this->data;
27+
}
28+
}

laravel/app/Listeners/TaskListener.php renamed to laravel/app/Listeners/TestListener.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Hhxsv5\LaravelS\Swoole\Task\Event;
1515
use Hhxsv5\LaravelS\Swoole\Task\Listener;
1616

17-
class TaskListener extends Listener
17+
class TestListener extends Listener
1818
{
1919
// 声明没有参数的构造函数
2020
public function __construct()
@@ -29,8 +29,8 @@ public function handle(Event $event)
2929
// 注意:
3030
// 1.参数2需传true
3131
// 2.config/laravels.php中修改配置 task_ipc_mode 为1或2,参考 https://wiki.swoole.com/wiki/page/296.html
32-
$ret = Task::deliver(new TaskEvent('task data'), true);
33-
var_dump($ret);
32+
// $ret = Task::deliver(new TaskEvent('task data'), true);
33+
// var_dump($ret);
3434
// throw new \Exception('an exception');// handle时抛出的异常上层会忽略,并记录到Swoole日志,需要开发者try/catch捕获处理
3535
}
3636
}

laravel/config/laravels.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
],
4141
// 绑定事件与监听器,一个事件可以有多个监听器,多个监听器按顺序执行
4242
'events' => [
43-
\App\Tasks\TaskEvent::class => [
44-
\App\Listeners\TaskListener::class,
43+
\App\Events\TestEvent::class => [
44+
\App\Listeners\TestListener::class,
4545
],
4646
],
4747
'swoole_tables' => [

0 commit comments

Comments
 (0)