File tree Expand file tree Collapse file tree 3 files changed +33
-5
lines changed Expand file tree Collapse file tree 3 files changed +33
-5
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 14
14
use Hhxsv5 \LaravelS \Swoole \Task \Event ;
15
15
use Hhxsv5 \LaravelS \Swoole \Task \Listener ;
16
16
17
- class TaskListener extends Listener
17
+ class TestListener extends Listener
18
18
{
19
19
// 声明没有参数的构造函数
20
20
public function __construct ()
@@ -29,8 +29,8 @@ public function handle(Event $event)
29
29
// 注意:
30
30
// 1.参数2需传true
31
31
// 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);
34
34
// throw new \Exception('an exception');// handle时抛出的异常上层会忽略,并记录到Swoole日志,需要开发者try/catch捕获处理
35
35
}
36
36
}
Original file line number Diff line number Diff line change 40
40
],
41
41
// 绑定事件与监听器,一个事件可以有多个监听器,多个监听器按顺序执行
42
42
'events ' => [
43
- \App \Tasks \TaskEvent ::class => [
44
- \App \Listeners \TaskListener ::class,
43
+ \App \Events \TestEvent ::class => [
44
+ \App \Listeners \TestListener ::class,
45
45
],
46
46
],
47
47
'swoole_tables ' => [
You can’t perform that action at this time.
0 commit comments