入门指引 Server Client Process AsyncIO Memory HttpServer WebSocket 高级 其他

发布于 2015-08-09 09:38:00 | 225 次阅读 | 评论: 0 | 来源: 网络整理

通过使用on方法注册异步回调函数。多个swoole_client可以嵌套回调。异步模式仅可用于cli模式,如在swoole_process或swoole_server中。

示例:

<?php
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); //异步非阻塞

$client->on("connect", function($cli) {
    echo "connectedn";
    $cli->send("hello worldn");
});

$client->on("receive", function($cli, $data) {
    if(empty($data)){
        $cli->close();
        echo "closedn";
    } else {
        echo "received: $datan";
        sleep(1);
        $cli->send("hellon");
    }
});

$client->on("error", function($cli){
    exit("errorn");
});

$client->on("close", function($cli){
    echo "connection is closedn";
});

$client->connect('127.0.0.1', 9501, 0.5);
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务