@@ -31,7 +31,7 @@ public function __construct()
3131 {
3232 }
3333
34- public function onOpen ($ server , $ request )
34+ public function onOpen (Server $ server , Request $ request )
3535 {
3636 $ userInfo = auth ('api ' )->user ();
3737 if (empty ($ userInfo )) {
@@ -47,54 +47,58 @@ public function onOpen($server, $request)
4747 $ action = $ req ['action ' ] ?? '' ;
4848 switch ($ action ) {
4949 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 ;
7061 }
71- if ($ count > 20000 ) { // 防止刷单的安全拦截
72- break ; // 超出就跳出循环
62+ // 每个用户 fd 限制请求次数
63+ $ redisKey = 'websocket_fd_ ' .$ request ->fd ;
64+ if (empty ($ this ->redis )) {
65+ $ this ->redis = Redis::connection ();
7366 }
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+ }
8185 }
8286 }
83- }
87+ });
8488 }
8589 return '' ;
8690
8791 // throw new \Exception('an exception');// 此时抛出的异常上层会忽略,并记录到Swoole日志,需要开发者try/catch捕获处理
8892 }
8993
90- public function onMessage ($ server , $ frame )
94+ public function onMessage (Server $ server , Frame $ frame )
9195 {
9296 // \Log::info('Received message', [$frame->fd, $frame->data, $frame->opcode, $frame->finish]);
9397 $ server ->push ($ frame ->fd , date ('Y-m-d H:i:s ' ));
9498 // throw new \Exception('an exception');// 此时抛出的异常上层会忽略,并记录到Swoole日志,需要开发者try/catch捕获处理
9599 }
96100
97- public function onClose ($ server , $ fd , $ reactorId )
101+ public function onClose (Server $ server , $ fd , $ reactorId )
98102 {
99103 // throw new \Exception('an exception');// 此时抛出的异常上层会忽略,并记录到Swoole日志,需要开发者try/catch捕获处理
100104 }
0 commit comments