This commit is contained in:
2023-11-30 17:02:20 +08:00
parent 0a705b27bc
commit 2ecb13c453
+17 -38
View File
@@ -5,7 +5,6 @@ namespace Kiri\Rpc;
use Exception; use Exception;
use JetBrains\PhpStorm\ArrayShape; use JetBrains\PhpStorm\ArrayShape;
use Kiri; use Kiri;
use Kiri\Di\LocalService;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Core\Json; use Kiri\Core\Json;
use Kiri\Events\EventProvider; use Kiri\Events\EventProvider;
@@ -62,26 +61,6 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
$this->eventProvider->on(OnBeforeShutdown::class, [$this, 'onBeforeShutdown']); $this->eventProvider->on(OnBeforeShutdown::class, [$this, 'onBeforeShutdown']);
$this->collector = $this->dataGrip->get('rpc'); $this->collector = $this->dataGrip->get('rpc');
$this->registerConsumers();
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function registerConsumers(): void
{
$consumers = \config('rpc.consumers', []);
if (!is_array($consumers)) {
return;
}
$local = $this->container->get(LocalService::class);
foreach ($consumers as $consumer) {
$local->set($consumer['id'], $consumer);
}
} }
@@ -196,23 +175,23 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
*/ */
private function dispatch($data): array private function dispatch($data): array
{ {
try { // try {
$class = $this->container->get(LocalService::class)->get($data['service']); // $class = $this->collector->query($data['service'], 'tcp');
if (!$this->container->has($class)) { // if (!$this->container->has($class)) {
throw new Exception('Handler not found', -32601); // throw new Exception('Handler not found', -32601);
} // }
$controller = $this->container->get($class); // $controller = $this->container->get($class);
if (!method_exists($controller, $data['method'])) { // if (!method_exists($controller, $data['method'])) {
throw new Exception('Method not found', -32601); // throw new Exception('Method not found', -32601);
} // }
if (!isset($data['params']) || !is_array($data['params'])) { // if (!isset($data['params']) || !is_array($data['params'])) {
$data['params'] = []; // $data['params'] = [];
} // }
return $this->handler($controller, $data['method'], $data['params']); // return $this->handler($controller, $data['method'], $data['params']);
} catch (\Throwable $throwable) { // } catch (\Throwable $throwable) {
$code = $throwable->getCode() == 0 ? -32603 : $throwable->getCode(); // $code = $throwable->getCode() == 0 ? -32603 : $throwable->getCode();
return $this->failure($code, jTraceEx($throwable), [], $data['id'] ?? null); // return $this->failure($code, jTraceEx($throwable), [], $data['id'] ?? null);
} // }
} }