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.

46 lines
1.1 KiB

<?php
$host = '127.0.0.1';
$port = 5188;
$data = '{"controller":"site","methor":"test","params":{"id":1}}';
$length = strlen($data);
$pack = pack('CNNNa*', 1, time(), 0, $length, $data);
$ret = Swoole\Coroutine::create(function()use($host, $port, $pack) {
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
$client->set(array(
'open_length_check' => true,
'package_max_length' => 81920,
'package_length_type' => 'N',
'package_length_offset' => 9,
'package_body_offset' => 13,
'connect_timeout' => 1.0,
'write_timeout' => 60.0,
'read_timeout' => 60.0
));
if (!$client->connect($host, $port)) {
echo "connect failed.\n";
return false;
}
@$client->send($pack);
$pack = @$client->recv();
if ($pack === false) {
echo $client->errCode . "\n";
return false;
} else if (empty($pack)) {
echo "close by peer.\n";
return false;
}
$ret = unpack('Ctype/Ntime/Nserid/Nlength/a*data', $pack);
$client->close();
return $ret['data'];
});
print_r($ret);