PHP操作swoole来实现实时异步任务队列

4 篇文章 0 订阅

看swoole的官方文档,真的很迷茫,文档里都是些零碎的点,这些点怎么串起来的,还得需要自己摸索。比如手册里将不同的进程分开来讲,但实际开发的过程中,使用却是一起使用的。比如任务队列。手册里的资料有一下几个地方 
https://wiki.swoole.com/wiki/page/481.html 
https://wiki.swoole.com/wiki/page/134.html 
https://wiki.swoole.com/wiki/page/54.html 
第一个是实例,但第二个和第三个页面确实从进程的角度去讲的,如果没有开发经验,的人真的很难明白是怎么回事。 
下面说下我的理解吧。 
server 函数列表https://wiki.swoole.com/wiki/page/15.html 
server事件列表https://wiki.swoole.com/wiki/page/41.html 
server中所有的函数都是主动发起一件事,而与之对应的,都有一个事件去接收。这就好比一个老板一个秘书,老板说我要去酒店,于是秘书就订酒店。对应到swoole中,函数就是老板,函数说我要发起一个任务,于是onTask事件,就开始工作了。很抽象,

安装

swoole扩展

pecl install swoole

swoole框架

composer install

运行

将client目录配置到Nginx/Apache的虚拟主机目录中,使client/index.html可访问。修改client/config.js中,IP和端口为对应的配置。

php webim_server.php

详细部署说明

1.安装composer(php依赖包工具)

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

注意:如果未将php解释器程序设置为环境变量PATH中,需要设置。因为composer文件第一行为#!/usr/bin/env php,并不能修改。更加详细的对composer说明:http://blog.csdn.net/zzulp/article/details/18981029

2.composer install

切换到PHPWebIM项目目录,执行指令composer install,如很慢则

composer install --prefer-dist

3.Ningx/Apache配置(这里未使用swoole_framework提供的Web AppServer)

nginx

server
{
    listen       80;
    server_name  im.swoole.com;
    index index.shtml index.html index.htm index.php;
    root  /path/to/PHPWebIM/client;
    location ~ .*\.(php|php5)?$
    {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
    access_log  /Library/WebServer/nginx/logs/im.swoole.com  access;
}

apache

<VirtualHost *:80>
    DocumentRoot "path/to/PHPWebIM/client"
    ServerName im.swoole.com
    AddType application/x-httpd-php .php
    <Directory />
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
        DirectoryIndex index.php
    </Directory>
</VirtualHost>

4.修改配置PHPWebIM/config.php

$config['server'] = array(
    //监听的HOST
    'host' => '0.0.0.0',
    //监听的端口
    'port' => '9503',
    //WebSocket的URL地址,供浏览器使用的
    'url' => 'ws://127.0.0.1:9503',
);

  • server.host server.port 项为WebIM服务器即WebSocket服务器的IP与端口,其他选择项根据具体情况修改
  • server.url对应的就是服务器IP或域名以及websocket服务的端口,这个就是提供给浏览器的WebSocket地址
  • webim.data_dir用于修改聊天记录存储的目录,必须有可写权限

5.启动WebSocket服务器

php PHPWebIM/webim_server.php

IE浏览器不支持WebSocket,需要使用FlashWebSocket模拟,请修改flash_policy.php中对应的端口,然后启动flash_policy.php。

php PHPWebIM/flash_policy.php

6.绑定host与访问聊天窗口(可选)

如果URL直接使用IP:PORT,这里不需要设置。

vi /etc/hosts

增加

127.0.0.1 im.swoole.com

用浏览器打开:http://im.swoole.com

//1.构建Server对象

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

//2.设置运行时参数

$serv->set(array(

'worker_num' => 8,

'daemonize' => 0,

'max_request' => 10000,

'dispatch_mode' => 2,

'debug_mode'=> 1,

));

//3.注册事件回调函数

$serv->on('Receive', function($serv, $fd, $from_id, $data){

$respData='<h1>Hello Swoole.</h1>';

response($serv,$fd,$respData);

//封装并发送HTTP响应报文

});

//4.启动服务器

$serv->start();



下面看代码

class msgServer
{
    private $serv;

    function __construct()
    {

        $this->serv = new Swoole\Server("127.0.0.1", 9501);//创建一个服务
        $this->serv->set(array('task_worker_num' => 4)); //配置task进程的数量
        $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($serv, $fd, $from_id, $data)
    {   
        $data = json_decode($data, true);
        $task_id = $serv->task($data);//这里发起了任务,于是上面的on('task', array($this, 'onTask'))就会执行

    }

    public function onTask($serv, $task_id, $from_id, $data)
    {
        $data['send_res'] = $this->sendMsg($data); //发送短信
        //1.7.3之前,是$serv->finish("result");
       return "result.";//这里告诉任务结束,于是上面的on('finish', array($this, 'onFinish'))就会执行
    }

    public function onFinish($serv, $task_id, $data)
    {
        $this->addSendLog($data); //添加短信发送记录
    }
}

$msgServ = new msgServer;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值