Use PHP to build an asynchronous service Swoole

Used in the project PHP, but because of the long time-consuming task, after the submission of the front end, you need the server asynchronous response.

Server asynchronously a variety of programs, including MQ, fsocket, Swoole and so on. (Recommended Learning: swoole video tutorial)

Swoole using pure C language, PHP language provides asynchronous multi-threaded server, asynchronous TCP / UDP network client, asynchronous MySQL , asynchronous Redis, database connection pool, AsyncTask, message queues, millisecond timer, asynchronous file read and write, asynchronous DNS queries. Swoole built Http / WebSocket server / client, HTTP2. 0 server side.

Most importantly, the perfect support for the PHP language. Thus Swoole used to build an asynchronous server provides asynchronous response, a push, a series of work scheduled tasks.

Swoole is written in C language, by way of compiled and installed.

Install dependencies are:

PHP -5.3.10 or later

gcc -4.4 or later

make

autoconf

pcre (centos system can execute the command: yum install PCRE - devel)

Installation:
 
phpize # If the command does not exist, please add the actual path in front of php

./configure

make 

sudo make install

After the compilation is complete, you need PHP . Add extensions in the ini

extension=swoole.so

Server

class Server{

    private $serv;

    public function __construct() {

        $this->serv = new swoole_server("0.0.0.0", 9501);

        $this->serv->set(array(

            // 'worker_num' =>. 1, generally set // the number of CPU servers 1-4 times

            'daemonize' => 1,   // executed in daemon

            'max_request' => 10000,

            'task_worker_num' => 1,   // Task number of processes

            "task_ipc_mode" =>. 3,   // Message Queuing communication, and set to mode competition

            'open_length_check'    => true,

            'dispatch_mode'        => 1,

            'package_length_type' => 'N',   // this is very critical, positioning the header

            'package_length_offset' => 0,       // N th byte is the value of the packet length

            'package_body_offset' =>. 4,       // byte it begins to calculate the length

            'package_max_length' => 2,000,000,   // protocol maximum length

            "log_file" => "/tmp/swoole_test.log"  //日志

        ));

        $this->serv->on('Receive', array($this, 'onReceive'));

        $this->serv->on('Task', array($this, 'onTask'));

        $this->serv->on('Finish', array($this, 'onFinish'));

        $this->serv->start();

    }

    public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {

        // into the task queue, started

        $task_id = $serv->task( $data );

    }

    public function onTask($serv,$task_id,$from_id, $data) {

      // do something

    }

Client

class Client{

    private $client, $ip, $port, $params;

    public function __construct($ip, $port, $params)

    {

        $this->ip = $ip;

        $this->port = $port;

        $this->params = $params;

        $this->client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);

        $this->client->set(array(

            'open_length_check'    => true,

            'package_length_type'  => 'N',

            'package_length_offset' => 0,       // N th byte is the value of the packet length

            'package_body_offset' =>. 4,       // byte it begins to calculate the length

            'package_max_length' => 2,000,000,   // protocol maximum length

        ));

        // set event callbacks

        $this->client->on('Connect', array($this, 'onConnect'));

        $this->client->on('Receive', array($this, 'onReceive'));

        $this->client->on('Close', array($this, 'onClose'));

        $this->client->on('Error', array($this, 'onError'));

        // initiate network connections

        $this->client->connect($ip, $port, 3);

    }

    public  function onReceive ( $ cli , $ data ) {

        echo "Received: " . $data . "\n";

    }

    public function onConnect($cli) {

        $data = pack('N', strlen($data)) . $data;

        $cli->send($data);

        $cli->close();

    }

    public function onClose( $cli)

    {

        echo "Connection close\n";

    }

    public function onError()

    {

        echo "Connect failed\n";

    }

}

 

Guess you like

Origin www.cnblogs.com/zzz2000/p/12639269.html