初識 swoole【下】

OMGZui發表於2018-11-07

前言:接初識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"