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.

45 lines
1.1 KiB

  1. <?php
  2. $host = '127.0.0.1';
  3. $port = 5188;
  4. $data = '{"controller":"site","methor":"test","params":{"id":1}}';
  5. $length = strlen($data);
  6. $pack = pack('CNNNa*', 1, time(), 0, $length, $data);
  7. $ret = Swoole\Coroutine::create(function()use($host, $port, $pack) {
  8. $client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
  9. $client->set(array(
  10. 'open_length_check' => true,
  11. 'package_max_length' => 81920,
  12. 'package_length_type' => 'N',
  13. 'package_length_offset' => 9,
  14. 'package_body_offset' => 13,
  15. 'connect_timeout' => 1.0,
  16. 'write_timeout' => 60.0,
  17. 'read_timeout' => 60.0
  18. ));
  19. if (!$client->connect($host, $port)) {
  20. echo "connect failed.\n";
  21. return false;
  22. }
  23. @$client->send($pack);
  24. $pack = @$client->recv();
  25. if ($pack === false) {
  26. echo $client->errCode . "\n";
  27. return false;
  28. } else if (empty($pack)) {
  29. echo "close by peer.\n";
  30. return false;
  31. }
  32. $ret = unpack('Ctype/Ntime/Nserid/Nlength/a*data', $pack);
  33. $client->close();
  34. return $ret['data'];
  35. });
  36. print_r($ret);