server = $server; $this->config = $config; } /** * @param $event * @author zfy * @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 zfy * @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); } } } }); } }