client = new Client(SWOOLE_SOCK_TCP); if (!$this->client->connect($this->host, $this->port, $this->timeout)) { throw new \RuntimeException("Cannot connect to Swoole Server {$this->host}:{$this->port}"); } } // 获取单例 public static function getInstance(): self { if (self::$instance === null) { self::$instance = new self(); } return self::$instance; } // 发送任务 public function sendTask(array $task): bool { $data = json_encode($task) . "\r\n"; return $this->client->send($data); } // 关闭连接 public function close(): void { $this->client->close(); self::$instance = null; } }