EasySwoole中使用异步客户端

为方便测试,我们以RPC中的例子来实现服务端,具体请看文档RPC章节。

纯原生异步

    public static function mainServerCreate(ServerManager $server,EventRegister $register): void
    {
        // TODO: Implement mainServerCreate() method.
        $tcp = $server->addServer('tcp',9502);
        $tcp->registerDefaultOnReceive(new \Tcp\Parser());
        $register->add($register::onWorkerStart,function ($ser,$workerId){
            if($workerId == 0){
                $client = new \swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
                $client->on("connect", function(\swoole_client $cli) {
                    $cli->send("test:delay");
                });
                $client->on("receive", function(\swoole_client $cli, $data){
                    echo "Receive: $data";
                    $cli->send("test:delay");
                    sleep(1);
                });
                $client->on("error", function(\swoole_client $cli){
                    echo "error\n";
                });
                $client->on("close", function(\swoole_client $cli){
                    echo "Connection close\n";
                });
                $client->connect('127.0.0.1', 9502);
            }
        });
    }

伪异步-eventLoop

利用swoole自带的事件循环,实现异步


    public static function mainServerCreate(ServerManager $server,EventRegister $register): void
    {
        // TODO: Implemen