Skip to content

Commit f4b004c

Browse files
committed
fix(control): websocket 查询数据
1 parent 359942e commit f4b004c

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

laravel/app/Services/WebSocketService.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace App\Services;
1010

11+
use App\Models\ApiExcel;
12+
use App\Models\ApiExcelLogs;
1113
use Hhxsv5\LaravelS\Swoole\WebSocketHandlerInterface;
1214
use Swoole\Http\Request;
1315
use Swoole\WebSocket\Frame;
@@ -27,14 +29,26 @@ public function onOpen($server, $request)
2729
{
2830
// 在触发 onOpen 事件之前 Laravel 的生命周期已经完结,所以 Laravel 的 Request 是可读的,Session 是可读写的
2931
// \Log::info('New WebSocket connection', [$request->fd, request()->all(), session()->getId(), session('xxx'), session(['yyy' => time()])]);
30-
$i = 1;
31-
while (true) {
32-
$server->push($request->fd, 'Welcome to LaravelS'.$i);
33-
sleep(1);
34-
$i++;
35-
if ($i > 10) {
36-
break;
32+
// 1. 根据 api_excel 的 id 查询总数,
33+
$req = $request->get;
34+
if (isset($req['id']) && $api_excel = ApiExcel::find($req['id'])) {
35+
if ($api_excel['state'] == 1 && $api_excel['total_excel'] > 0) {
36+
// 2. 查询 api_excel_logs 表更新的数据量
37+
while (true) {
38+
$total = ApiExcelLogs::where('api_excel_id', $req['id'])->count();
39+
$str = floor($total / $api_excel['total_excel'] * 100).'%';
40+
// 3. 输出完成率
41+
$server->push($request->fd, $str);
42+
sleep(1);
43+
if ($total >= $api_excel['total_excel']) {
44+
break;
45+
}
46+
}
47+
} else {
48+
$server->push($request->fd, '100%');
3749
}
50+
} else {
51+
$server->push($request->fd, '100%');
3852
}
3953
// throw new \Exception('an exception');// 此时抛出的异常上层会忽略,并记录到Swoole日志,需要开发者try/catch捕获处理
4054
}

0 commit comments

Comments
 (0)