Skip to content

Commit 19fd15d

Browse files
committed
update: webSocket
1 parent af8dbc8 commit 19fd15d

File tree

2 files changed

+9
-36
lines changed

2 files changed

+9
-36
lines changed

laravel/app/Http/Repository/MultithreadingRepository.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -528,27 +528,23 @@ public function getExcelData($fileName)
528528
*
529529
* @param $excel_id
530530
*
531-
* @return string
531+
* @return string|bool
532532
*/
533533
public function completionRate($excel_id)
534534
{
535535
if ($api_excel = ApiExcel::find($excel_id)) {
536536
if ($api_excel['state'] == 1) {
537-
$total_excel = cacheTotalExcel($excel_id, $api_excel['upload_url']);
537+
$total_excel = cacheTotalExcel($excel_id, public_path($api_excel['upload_url']));
538538
if ($total_excel > 0) {
539539
// 2. 查询 api_excel_logs 表更新的数据量
540540
$total = ApiExcelLogs::where('api_excel_id', $excel_id)->count();
541541
// 3. 返回完成率
542-
return floor($total / $api_excel['total_excel'] * 100).'%';
543-
} else {
544-
return '100%';
542+
return floor($total / $api_excel['total_excel'] * 100);
545543
}
546-
} else {
547-
return '';
548544
}
549-
} else {
550-
return '100%';
551545
}
546+
547+
return '100';
552548
}
553549

554550
/**

laravel/app/Services/WebSocketService.php

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,40 +37,17 @@ public function onOpen($server, $request)
3737
while (true) {
3838
// 3. 输出完成率
3939
$rate = MultithreadingRepository::getInstent()->completionRate($req['id']);
40-
$server->push($request->fd, $rate);
40+
$server->push($request->fd, $rate.'%');
4141
sleep(3);
42-
if ($rate) {
43-
break;
44-
}
45-
}
46-
}
4742

48-
49-
if (isset($req['id']) && $api_excel = ApiExcel::find($req['id'])) {
50-
if ($api_excel['state'] == 1) {
51-
$total_excel = cacheTotalExcel($req['id'], $api_excel['upload_url']);
52-
if ($total_excel > 0) {
53-
// 2. 查询 api_excel_logs 表更新的数据量
54-
while (true) {
55-
$total = ApiExcelLogs::where('api_excel_id', $req['id'])->count();
56-
$str = floor($total / $api_excel['total_excel'] * 100).'%';
57-
// 3. 输出完成率
58-
$server->push($request->fd, $str);
59-
sleep(3);
60-
if ($total >= $api_excel['total_excel']) {
61-
break;
62-
}
63-
}
64-
} else {
65-
$server->push($request->fd, '100%');
43+
if ($rate >= 100) {
44+
break;
6645
}
67-
68-
} else {
69-
$server->push($request->fd, '0%');
7046
}
7147
} else {
7248
$server->push($request->fd, '100%');
7349
}
50+
7451
// throw new \Exception('an exception');// 此时抛出的异常上层会忽略,并记录到Swoole日志,需要开发者try/catch捕获处理
7552
}
7653

0 commit comments

Comments
 (0)