InteractsWithRpcServer.php 834 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace think\swoole\concerns;
  3. use Swoole\Server;
  4. use think\App;
  5. use think\swoole\Pool;
  6. use think\swoole\rpc\Manager;
  7. /**
  8. * Trait InteractsWithRpc
  9. * @package think\swoole\concerns
  10. * @property App $app
  11. * @property App $container
  12. * @method Server getServer()
  13. * @method Pool getPools()
  14. */
  15. trait InteractsWithRpcServer
  16. {
  17. protected function prepareRpcServer()
  18. {
  19. if ($this->getConfig('rpc.server.enable', false)) {
  20. $host = $this->getConfig('server.host');
  21. $port = $this->getConfig('rpc.server.port', 9000);
  22. $rpcServer = $this->getServer()->addlistener($host, $port, SWOOLE_SOCK_TCP);
  23. /** @var Manager $rpcManager */
  24. $rpcManager = $this->container->make(Manager::class);
  25. $rpcManager->attachToServer($rpcServer);
  26. }
  27. }
  28. }