This commit is contained in:
2026-02-26 14:39:04 +08:00
parent c0a61aebdd
commit d90d2eb844
6 changed files with 463 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
<?php
namespace Coroutine\Server;
use Kiri\Di\Inject\Container;
use Kiri\NoSql\Redis;
use Swoole\Coroutine;
use Swoole\Coroutine\Http\Client;
class QueueLoop
{
#[Container(Transport::class)]
public Transport $transport;
/**
* @return void
*/
public function loop(): void
{
$this->cleanOnline();
$redis = \Kiri::getDi()->get(Redis::class);
if ($redis instanceof Redis) {
$data = $redis->blPop(\config('redis.key'), 10);
if (!empty($data)) {
[$key, $data] = $data;
$json = json_decode($data, true);
$this->transport->send($json['userId'], json_encode($json['data']));
}
} else {
Coroutine::sleep(0.05);
}
$this->loop();
}
/**
* @return void
*/
public function cleanOnline(): void
{
$client = new Client(config('notice.host'), config('notice.port'));
$client->post('/cleanOnline', []);
$client->close();
}
}