截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Client.php 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace think\swoole\pool;
  3. use Smf\ConnectionPool\Connectors\ConnectorInterface;
  4. use think\helper\Arr;
  5. class Client implements ConnectorInterface
  6. {
  7. /**
  8. * Connect to the specified Server and returns the connection resource
  9. * @param array $config
  10. * @return \Swoole\Coroutine\Client
  11. */
  12. public function connect(array $config)
  13. {
  14. $client = new \Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
  15. $host = Arr::pull($config, 'host');
  16. $port = Arr::pull($config, 'port');
  17. $timeout = Arr::pull($config, 'timeout', 5);
  18. $client->set($config);
  19. $client->connect($host, $port, $timeout);
  20. return $client;
  21. }
  22. /**
  23. * Disconnect and free resources
  24. * @param \Swoole\Coroutine\Client $connection
  25. * @return mixed
  26. */
  27. public function disconnect($connection)
  28. {
  29. $connection->close();
  30. }
  31. /**
  32. * Whether the connection is established
  33. * @param \Swoole\Coroutine\Client $connection
  34. * @return bool
  35. */
  36. public function isConnected($connection): bool
  37. {
  38. return $connection->isConnected() && $connection->peek() !== '';
  39. }
  40. /**
  41. * Reset the connection
  42. * @param \Swoole\Coroutine\Client $connection
  43. * @param array $config
  44. * @return mixed
  45. */
  46. public function reset($connection, array $config)
  47. {
  48. }
  49. /**
  50. * Validate the connection
  51. *
  52. * @param \Swoole\Coroutine\Client $connection
  53. * @return bool
  54. */
  55. public function validate($connection): bool
  56. {
  57. return $connection instanceof \Swoole\Coroutine\Client;
  58. }
  59. }