类别:Swoole / 日期:2019-12-16 / 浏览:225 / 评论:0

项目中运用的PHP,但由于长耗时的使命,前端提交今后,须要服务端异步相应。

服务器异步有多种计划,包含MQ,fsocket,Swoole等。 (引荐进修: swoole视频教程)

Swoole 运用纯 C 言语编写,供应了 PHP 言语的异步多线程服务器,异步 TCP/UDP 收集客户端,异步 MySQL,异步 Redis,数据库连接池,AsyncTask,音讯行列,毫秒定时器,异步文件读写,异步DNS查询。 Swoole内置了Http/WebSocket服务器端/客户端、Http2.0服务器端。

最主要的是,圆满支撑PHP言语。因而运用Swoole搭建了一个异步服务器,供应异步相应,推送,定时使命等一系列事变。

Swoole是C言语编写,采纳编译装置的体式格局。

装置依靠项有:

php-5.3.10 或更高版本
gcc-4.4 或更高版本
make
autoconf
pcre (centos体系能够实行敕令:yum install pcre-devel)

装置体式格局:

phpize #假如敕令不存在 请在前面加上php的现实途径
./configure
make 
sudo make install

编译完成今后,须要在php.ini中增加扩大

extension=swoole.so

服务端

class Server{
    private $serv;
    public function __construct() {
        $this->serv = new swoole_server("0.0.0.0", 9501);
        $this->serv->set(array(
            //'worker_num' => 1,  //平常设置为服务器CPU数的1-4倍
            'daemonize' => 1,  //以保卫历程实行
            'max_request' => 10000,
            'task_worker_num' => 1,  //task历程的数目
            "task_ipc_mode " => 3 ,  //运用音讯行列通讯,并设置为争抢形式
            'open_length_check'    => true,
            'dispatch_mode'        => 1,
            'package_length_type'  => 'N',  //这个很症结,定位包头的
            'package_length_offset' => 0,      //第N个字节是包长度的值
            'package_body_offset'  => 4,      //第几个字节入手下手计算长度
            'package_max_length'    => 2000000,  //协定最大长度
            "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 ) {
        //放入使命行列,入手下手实行
        $task_id = $serv->task( $data );
    }
    public function onTask($serv,$task_id,$from_id, $data) {
      //做一些事变
    }

客户端

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个字节是包长度的值
            'package_body_offset'  => 4,      //第几个字节入手下手计算长度
            'package_max_length'    => 2000000,  //协定最大长度
        ));
        //设置事宜回调函数
        $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'));
        //提议收集连接
        $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";
    }
}

以上就是PHP swoole怎样用的细致内容,更多请关注ki4网别的相干文章!

打赏

感谢您的赞助~

打开支付宝扫一扫,即可进行扫码打赏哦~

版权声明 : 本文未使用任何知识共享协议授权,您可以任何形式自由转载或使用。

 可能感兴趣的文章