- Coroutine\Client
- 继承关系
- 使用实例
- 协议处理
Coroutine\Client
Coroutine\Client提供了TCP、UDP、UnixSocket传输协议的Socket客户端封装代码,使用时仅需new Swoole\Coroutine\Client即可。
Coroutine\Client底层实现协程调度,业务层无需感知- 使用方法和
Client同步模式方法完全一致 connect超时设置同时作用于Connect和Recv、Send超时- 除了正常的调用外,
Coroutine\Client还支持并行请求
继承关系
Coroutine\Client与Client并不是继承关系,但绝大部分Client提供的方法均可在Coroutine\Client中使用。请参考 Client 。
在Coroutine\Client中可以使用set方法设置配置选项,使用方法和与Client->set完全一致,请参考 Client,对于使用有区别的函数,此处单独说明
使用实例
use Swoole;$client = new Coroutine\Client(SWOOLE_SOCK_TCP);if (!$client->connect('127.0.0.1', 9501, 0.5)){exit("connect failed. Error: {$client->errCode}\n");}$client->send("hello world\n");echo $client->recv();$client->close();
协议处理
协程客户端也支持长度和EOF协议处理,设置方法与 Client 完全一致。
use Swoole;$client = new Coroutine\Client(SWOOLE_SOCK_TCP);$client->set(array('open_length_check' => 1,'package_length_type' => 'N','package_length_offset' => 0, //第N个字节是包长度的值'package_body_offset' => 4, //第几个字节开始计算长度'package_max_length' => 2000000, //协议最大长度));
