async_client.php 840 B

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. function send(swoole_client $cli)
  3. {
  4. $_send = str_repeat('A', rand(10000, 50000)) . "\r\n\r\n";
  5. $cli->send($_send);
  6. echo "send ".strlen($_send)." bytes\n";
  7. }
  8. $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); //异步非阻塞
  9. $client->set(array('open_eof_check' => true, 'package_eof' => "\r\n\r\n"));
  10. $client->on("connect", function(swoole_client $cli) {
  11. send($cli);
  12. });
  13. $client->on("receive", function (swoole_client $cli, $data) {
  14. static $i = 0;
  15. if ($i % 100 == 1)
  16. {
  17. echo "received " . strlen($data) . " bytes\n";
  18. }
  19. $i ++;
  20. //usleep(200000);
  21. //send($dpcli);
  22. });
  23. $client->on("error", function(swoole_client $cli){
  24. echo "error\n";
  25. });
  26. $client->on("close", function(swoole_client $cli){
  27. echo "Connection close\n";
  28. });
  29. $client->connect('127.0.0.1', 9501);