connect($this->host, $this->port, $this->timeout)) { throw new \RuntimeException( "Cannot connect to Swoole Server {$this->host}:{$this->port}, error: {$client->errCode}" ); } $data = json_encode($task, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); if ($data === false) { throw new \RuntimeException('JSON encode failed: ' . json_last_error_msg()); } // 你的服务端使用 \r\n 作为 package_eof,所以这里必须加 \r\n $sendResult = $client->send($data . "\r\n"); return $sendResult !== false; } finally { if ($client->isConnected()) { $client->close(); } } } // 保留 close 方法,避免旧代码调用时报错 public function close(): void { self::$instance = null; } }