旗下导航:搜·么
当前位置:网站首页 > PHP框架 > Workerman > 正文

workerman怎样搭配tp【workerman】,workerman

作者:搜搜PHP网发布时间:2019-12-05分类:Workerman浏览:57


导读:下载workerman,放入thinkphp,注重与Home平级,就把workerman当作是一个模块。进入Home/Controller目次,新建WorkermanCon...

下载workerman,放入thinkphp,注重与Home平级,就把workerman当作是一个模块。进入Home/Controller目次,新建WorkermanController.class.php。 (引荐进修: workerman教程)

直接上源码,这里我是以保卫历程体式格局运转的,调试的话,能够去掉daemonize = true 这行。

<?php
namespace Home\Controller;
use Workerman\Worker;
/**
 * 用户信息查询
 */
class WorkermanController{
    /**
    * 用户信息查询
    */
    public function index(){
        if(!IS_CLI){
            die("access illegal");
        }
        require_once APP_PATH.'Workerman/Autoloader.php';
        define('MAX_REQUEST', 1000);// 每一个历程最多实行1000个要求
        Worker::$daemonize = true;//以保卫历程运转
        Worker::$pidFile = '/data/wwwlogs/Worker/workerman.pid';//轻易监控WorkerMan历程状况
        Worker::$stdoutFile = '/data/wwwlogs/Worker/stdout.log';//输出日记, 如echo,var_dump等
        Worker::$logFile = '/data/wwwlogs/Worker/workerman.log';//workerman本身相干的日记,包括启动、住手等,不包括任何营业日记
        $worker = new Worker('text://172.16.0.10:10024');//此处我运用内网ip
        $worker->name = 'Worker';
        $worker->count = 2;
        //$worker->transport = 'udp';// 运用udp协定,默许TCP
        $worker->onWorkerStart = function($worker){
            echo "Worker starting...\n";
        };
        $worker->onMessage = function($connection, $data){
            static $request_count = 0;// 已处置惩罚要求数
            //$_rs=D("Article")->gettest();
            $_articleObj=A("article");
            $_rs=$_articleObj->gettest();
            var_dump($_rs);
            $connection->send("hello");
            /*
             * 退出当前历程,主历程会马上重新启动一个全新历程补充上来,从而完成历程重启
             */
            if(++$request_count >= MAX_REQUEST){// 假如要求数到达1000
                Worker::stopAll();
            }
        };
        $worker->onBufferFull = function($connection){
            echo "bufferFull and do not send again\n";
        };
        $worker->onBufferDrain = function($connection){
            echo "buffer drain and continue send\n";
        };
        $worker->onWorkerStop = function($worker){
            echo "Worker stopping...\n";
        };
        $worker->onError = function($connection, $code, $msg){
            echo "error $code $msg\n";
        };
        // 运转worker
        Worker::runAll();
    }
}

修正Workerman/Worker.php源码,找到parseCommand()要领,workerman版本3.3.2的话,在586行,修正命令行检测语法:

<?php
protected static function parseCommand()
{
    global $argv;
    // Check argv;
    $start_file = $argv[0];
    if (!isset($argv[2])) { //修正了此处
       exit("Usage: php yourfile.php Controller/Action {start|stop|restart|reload|status|kill}\n");//修正了此处提醒
    }
    // Get command.
    $command  = trim($argv[2]);//修正了此处
    $command2 = isset($argv[3]) ? $argv[3] : '';//修正了此处
   ....
}

OK,此时功德圆满。

Linux命令行下运转,注重,此处要切换到thinkphp根目次下面

/usr/local/php/bin/php index.php Workerman/index start

检察运转状况:

/usr/local/php/bin/php index.php Workerman/index status

以上就是workerman怎样搭配tp的细致内容,更多请关注ki4网别的相干文章!

标签:workerman