× 警告!旧版文档已经暂停维护,请查看新版文档。点击前往新版文档

Coroutine\Client

Coroutine\Client提供了TCPUDPUnixSocket传输协议的Socket客户端封装代码,使用时仅需new Swoole\Coroutine\Client即可。

  • Coroutine\Client底层实现协程调度,业务层无需感知
  • 使用方法和Client同步模式方法完全一致
  • connect超时设置同时作用于ConnectRecvSend 超时
  • 除了正常的调用外,Coroutine\Client还支持并行请求

继承关系

Coroutine\ClientClient并不是继承关系,但绝大部分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,  //协议最大长度
));

  • 13262732358

    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(); 无法运行报错 这代码要在什么位置执行?

  • 13262732358

    Stack trace:

    0 Swoole\Coroutine\Client->connect() called at [/mnt/hgfs/WWW/yichenSwooleMsg/application/chat/controller/server2.php:139]

  • 13262732358

    client 怎样连接wss 和ws

  • 18301497790

    use Swoole

    Warning : The use statement with non-compound name 'Swoole' has no effect

  • 13925185624

    要在协程客户端的代码要在协程内运行这些代码