2

前言:接初识swoole【上】,这篇主要是异步问题

有收获的话请加颗小星星,没有收获的话可以 反对 没有帮助 举报三连

6、异步任务

<?php
// new
$server = new swoole_server(HOST, "9505",SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
//设置异步任务的工作进程数量
$server->set(['task_worker_num' => 4]);
// connect 连接
$server->on("connect", function (swoole_server $server, $fd) {
    dump("{$fd}连接");
    $server->send($fd, "欢迎{$fd}大山驴\n");
});
// receive 回调
$server->on("receive", function (swoole_server $server, $fd, $from_id, $data) {
    //投递异步任务
    $task_id = $server->task($data);
    dump("触发异步任务ID={$task_id}");
    $server->send($fd, "服务端回复:{$data}\n");
    foreach ($server->connections as $connection) {
        if ($connection != $fd){
            $server->send($connection, "{$fd}说{$data}");
        }
    }
});
// task 处理异步任务
$server->on("task", function (swoole_server $server, $task_id, $from_id, $data){
    dump("新的异步任务[ID={$task_id}]");
    //返回任务执行的结果
    $server->finish("{$data}完成了");
});
// finish 处理异步任务的结果
$server->on("finish", function (swoole_server $server, $task_id, $data){
    dump("异步任务[{$task_id}]已经完成[{$data}]");
});
// close
$server->on("close", function (swoole_server $server, $fd) {
    dump("{$fd}关闭");
    foreach ($server->connections as $connection) {
        if ($connection != $fd){
            $server->send($connection, "{$fd}断开连接");
        }
    }
});
// start
$server->start();
php async.php

telnet 127.0.0.1 9505

7、同步TCP客户端

<?php
// new 同步
$client = new \Swoole\Client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
// connect
if (! $client->connect(HOST, 9501, 0.5)){
    dump("连接失败");
}
// send
if (! $client->send("你个山驴逼\n")) {
    dump("发送失败");
}
// receive
if (! $data = $client->recv()) {
    dump("接收失败");
}
dump($data);
// close
$client->close();
php tcp_server.php

php tcp_sync_client.php

8、异步TCP客户端

<?php
// new 异步
$client = new \Swoole\Client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
// connect
$client->on("connect", function (\Swoole\Client $cli){
    $cli->send("你个山驴逼\n");
});
// receive
$client->on("receive", function (\Swoole\Client $cli, $data){
    dump("接收:{$data}");
});
// error
$client->on("error", function (\Swoole\Client $cli){
    dump("连接失败");
});
// close
$client->on("close", function (\Swoole\Client $cli){
    dump("连接关闭");
});
$client->connect(HOST, 9501, 0.5);
php tcp_server.php

php tcp_async_client.php

9、异步客户端

mysql

// 异步mysql客户端
$db = new \Swoole\Mysql();
$config = [
    'host' => '127.0.0.1',
    'user' => 'root',
    'password' => 'root',
    'database' => 'mac',
    'port' => '3307',
];

$db->connect($config, function (\Swoole\Mysql $db, $rs) {
    $db->query('SELECT * FROM user', function (\Swoole\Mysql $db, $rs) {
        dump($rs);
        $db->close();
    });
});

http

// 异步http客户端
$cli = new Swoole\Http\Client('127.0.0.1', 80);
$cli->setHeaders(array('User-Agent' => 'swoole-http-client'));
$cli->setCookies(array('test' => 'value'));

$cli->get('/login', function (\Swoole\Http\Client $cli) {
    dump($cli->statusCode);
    dump($cli->cookies);
    dump($cli->headers);
});

10、协程客户端

// 协程mysql客户端
$http = new swoole_http_server(HOST, 9506);
$http->on('request', function ($request, $response) {
    $db = new \Swoole\Coroutine\Mysql();
    $db->connect([
        'host' => '127.0.0.1',
        'user' => 'root',
        'password' => 'root',
        'database' => 'mac',
        'port' => '3307',
    ]);
    $data = $db->query('select * from user');
    dump($data);
    $response->end(json_encode($data));
});
$http->start();
php coroutine_client.php

curl -XGET "127.0.0.1:9506"

OMGZui
651 声望25 粉丝

Is Engineer, not Programmer