[Video Tutorial] latest version swoole installation and functional testing TASKS

Multiple concurrent high-performance network communication expansion Today we install and test php, and this extension is developed using C voice, later loaded into PHP, achieved multiple concurrent asynchronous communication on PHP level, many features go simulate speech greatly broadens the PHP application scenarios.

Direct use of the official website of the phrase can command, errors may occur and jamming does not move when you install swoole, try more than once can be successful.
pecl install swoole
To configure the environment and cli ini files php fpm environment, and to extend the so loaded inside.

Video Address:

https://www.bilibili.com/video/av70354024/

httpServer.php

$http = new swoole_http_server("127.0.0.1", 9501);

$http->on("start", function ($server) {
        echo "Swoole http server is started at http://127.0.0.1:9501\n";
});

$http->on("request", function ($request, $response) {
        $response->header("Content-Type", "text/plain");
            $response->end("Hello World\n");
});

$http->start();

Note the use of httpClient when there will be some errors, the first new version of swoole removed Swoole \ Http \ Client, the class can not be found will be reported, and secondly to perform the get method in the Association process

httpClient.php

$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function($cli) {
	    $cli->send("hello world\n");
});
$client->on("receive", function($cli, $data){
	    echo "received: {$data}\n";
});
$client->on("error", function($cli){
	    echo "connect failed\n";
});
$client->on("close", function($cli){
	    echo "connection close\n";
});
$client->connect("127.0.0.1", 9502, 0.5);

taskServer.php

<?php
$serv = new Swoole\Server("127.0.0.1", 9502, SWOOLE_BASE);

$serv->set(array(
    'worker_num' => 2,
    'task_worker_num' => 4,
));

$serv->on('Receive', function(Swoole\Server $serv, $fd, $from_id, $data) {
    echo "接收数据" . $data . "\n";
    $data = trim($data);
    $task_id = $serv->task($data, 0); 
    $serv->send($fd, "分发任务,任务id为$task_id\n");
});

$serv->on('Task', function (Swoole\Server $serv, $task_id, $from_id, $data) {
    echo "Tasker进程接收到数据";
    echo "#{$serv->worker_id}\tonTask: [PID={$serv->worker_pid}]: task_id=$task_id, data_len=".strlen($data).".".PHP_EOL;
    $serv->finish($data);
});

$serv->on('Finish', function (Swoole\Server $serv, $task_id, $data) {
    echo "Task#$task_id finished, data_len=".strlen($data).PHP_EOL;
});

$serv->on('workerStart', function($serv, $worker_id) {
    global $argv;
    if($worker_id >= $serv->setting['worker_num']) {
        swoole_set_process_name("php {$argv[0]}: task_worker");
    } else {
        swoole_set_process_name("php {$argv[0]}: worker");
    }
});

$serv->start();

  

taskClient.php

$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function($cli) {
        $cli->send("hello world\n");
});
$client->on("receive", function($cli, $data){
        echo "received: {$data}\n";
});
$client->on("error", function($cli){
        echo "connect failed\n";
});
$client->on("close", function($cli){
        echo "connection close\n";
});
$client->connect("127.0.0.1", 9502, 0.5);

 

  

Guess you like

Origin www.cnblogs.com/taoshihan/p/11628453.html