pack; if (is_callable($callback, true)) { return $callback($data); } return JSON::encode($data); } /** * @param $data * @return mixed */ public function unpack($data) { $callback = $this->unpack; if (is_callable($callback, true)) { return $callback($data); } return JSON::decode($data); } /** * @param Server $server * @param int $fd * @param int $reactorId * @param string $data * @return mixed * @throws Exception */ public function onHandler(\Swoole\Server $server, int $fd, int $reactorId, string $data) { try { $client = [$fd]; if (empty($data = $this->unpack($data))) { throw new Exception('Format error.'); } $client[] = $this->pack($data); return $server->send(...$client); } catch (\Throwable $exception) { $client[] = $this->pack(['message' => $exception->getMessage()]); return $server->send(...$client); } finally { $event = Snowflake::app()->event; $event->trigger(Event::SERVER_WORKER_STOP); } } }