@@ -31,7 +31,7 @@ public function __construct()
31
31
{
32
32
}
33
33
34
- public function onOpen ($ server , $ request )
34
+ public function onOpen (Server $ server , Request $ request )
35
35
{
36
36
$ userInfo = auth ('api ' )->user ();
37
37
if (empty ($ userInfo )) {
@@ -47,54 +47,58 @@ public function onOpen($server, $request)
47
47
$ action = $ req ['action ' ] ?? '' ;
48
48
switch ($ action ) {
49
49
case 'api_excel ' : // api_excel 列表完成率
50
- while (true ) {
51
- $ user_id = $ userInfo ['id ' ];
52
- $ server ->push ($ request ->fd , $ this ->apiExcel ($ user_id ));
53
- sleep (5 );
54
- $ state = ApiExcel::where ('state ' , 1 )->first ();
55
- if (!$ state ) {
56
- $ server ->push ($ request ->fd , $ this ->apiExcel ($ user_id ));
57
- break ;
58
- }
59
- // 每个用户 fd 限制请求次数
60
- $ redisKey = 'websocket_fd_ ' .$ request ->fd ;
61
- if (empty ($ this ->redis )) {
62
- $ this ->redis = Redis::connection ();
63
- }
64
- // 如果获取不到 redis 实例,使用总计数次数
65
- if ($ this ->redis ) {
66
- $ count = $ this ->redis ->incr ($ redisKey );
67
- if ($ count == 1 ) {
68
- // 设置过期时间
69
- $ this ->redis ->expire ($ redisKey , 6000 );
50
+ $ user_id = $ userInfo ['id ' ];
51
+ $ server ->push ($ request ->fd , $ this ->apiExcel ($ user_id ));
52
+ go (function () use ($ server , $ request , $ user_id ) {
53
+ while (true ) {
54
+ // 创建协程 - 睡眠操作影响 worker 进程
55
+ // Coroutine::sleep(5);
56
+ sleep (5 );
57
+ $ state = ApiExcel::where ('state ' , 1 )->first ();
58
+ if (!$ state ) {
59
+ $ server ->push ($ request ->fd , $ this ->apiExcel ($ user_id ));
60
+ return ;
70
61
}
71
- if ($ count > 20000 ) { // 防止刷单的安全拦截
72
- break ; // 超出就跳出循环
62
+ // 每个用户 fd 限制请求次数
63
+ $ redisKey = 'websocket_fd_ ' .$ request ->fd ;
64
+ if (empty ($ this ->redis )) {
65
+ $ this ->redis = Redis::connection ();
73
66
}
74
- } else {
75
- $ count_fd = 'count_ ' .$ request ->fd ;
76
- $ this ->incrKey ($ count_fd );
77
- // 单fd超过 1000 次跳出循环
78
- if ($ this ->$ count_fd > 1000 ) {
79
- unset($ this ->$ count_fd );
80
- break ;
67
+ // 如果获取不到 redis 实例,使用总计数次数
68
+ if ($ this ->redis ) {
69
+ $ count = $ this ->redis ->incr ($ redisKey );
70
+ if ($ count == 1 ) {
71
+ // 设置过期时间
72
+ $ this ->redis ->expire ($ redisKey , 600 );
73
+ }
74
+ if ($ count > 20000 ) { // 防止刷单的安全拦截
75
+ return ; // 超出就跳出循环
76
+ }
77
+ } else {
78
+ $ count_fd = 'count_ ' .$ request ->fd ;
79
+ $ this ->incrKey ($ count_fd );
80
+ // 单fd超过 1000 次跳出循环
81
+ if ($ this ->$ count_fd > 1000 ) {
82
+ unset($ this ->$ count_fd );
83
+ return ;
84
+ }
81
85
}
82
86
}
83
- }
87
+ });
84
88
}
85
89
return '' ;
86
90
87
91
// throw new \Exception('an exception');// 此时抛出的异常上层会忽略,并记录到Swoole日志,需要开发者try/catch捕获处理
88
92
}
89
93
90
- public function onMessage ($ server , $ frame )
94
+ public function onMessage (Server $ server , Frame $ frame )
91
95
{
92
96
// \Log::info('Received message', [$frame->fd, $frame->data, $frame->opcode, $frame->finish]);
93
97
$ server ->push ($ frame ->fd , date ('Y-m-d H:i:s ' ));
94
98
// throw new \Exception('an exception');// 此时抛出的异常上层会忽略,并记录到Swoole日志,需要开发者try/catch捕获处理
95
99
}
96
100
97
- public function onClose ($ server , $ fd , $ reactorId )
101
+ public function onClose (Server $ server , $ fd , $ reactorId )
98
102
{
99
103
// throw new \Exception('an exception');// 此时抛出的异常上层会忽略,并记录到Swoole日志,需要开发者try/catch捕获处理
100
104
}
0 commit comments