<?php




namespace app\webscoket;


use ln\interfaces\ListenerInterface;
use Swoole\Server;
use Swoole\Timer;
use think\Config;

/**
 * Class SwooleWorkerStart
 * @package app\webscoket
 * @author xaboy
 * @day 2020-04-29
 */
class SwooleWorkerStart implements ListenerInterface
{

    /**
     * @var \Swoole\WebSocket\Server
     */
    protected $server;

    /**
     * @var Config
     */
    protected $config;

    /**
     * SwooleWorkerStart constructor.
     * @param Server $server
     * @param Config $config
     */
    public function __construct(Server $server, Config $config)
    {
        $this->server = $server;
        $this->config = $config;
    }

    /**
     * @param $event
     * @author xaboy
     * @day 2020-04-29
     */
    public function handle($event): void
    {
        if (!env('INSTALLED', false)) return;
        if ($this->server->worker_id == ($this->config->get('swoole.server.options.worker_num')) && $this->config->get('swoole.websocket.enable', false)) {
            $this->ping();
        }
    }

    /**
     * @author xaboy
     * @day 2020-05-06
     */
    protected function ping()
    {
        /**
         * @var $pingService Ping
         */
        $pingService = app()->make(Ping::class);
        $server = $this->server;
        $timeout = (int)($this->config->get('swoole.websocket.ping_timeout', 60000) / 1000);
        Timer::tick(1500, function (int $timer_id) use (&$server, &$pingService, $timeout) {
            $nowTime = time();
            foreach ($server->connections as $fd) {
                if ($server->isEstablished($fd) && $server->exist($fd)) {
                    $last = $pingService->getLastTime($fd);
                    if ($last && ($nowTime - $last) > $timeout) {
                        $server->close($fd);
                    }
                }
            }
        });
    }
}
