Skip to content

Commit 1c5cd21

Browse files
committed
fix 还原 websocket
1 parent 7ff0fa4 commit 1c5cd21

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

laravel/app/Services/WebSocketService.php

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,44 +47,40 @@ public function onOpen(Server $server, Request $request)
4747
$action = $req['action'] ?? '';
4848
switch ($action) {
4949
case 'api_excel': // api_excel 列表完成率
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 '';
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);
6170
}
62-
// 每个用户 fd 限制请求次数
63-
$redisKey = 'websocket_fd_'.$request->fd;
64-
if (empty($this->redis)) {
65-
$this->redis = Redis::connection();
71+
if ($count > 20000) { // 防止刷单的安全拦截
72+
break; // 超出就跳出循环
6673
}
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-
}
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;
8581
}
8682
}
87-
// });
83+
}
8884
}
8985
return '';
9086

0 commit comments

Comments
 (0)