48 lines
873 B
PHP
48 lines
873 B
PHP
|
|
<?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();
|
||
|
|
}
|
||
|
|
}
|