PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > Laravel教程 > 正文

    laravel项目整合workerman长连接

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:首先运行命令检测当前cli环境是否支持:curl -Ss http://www.workerman.net/check.php | phpphp -m //查看当前cli环境php模块某些集成...
    首先运行命令检测当前cli环境是否支持:

    curl -Ss http://www.workerman.net/check.php | php
    php -m //查看当前cli环境php模块

    某些集成环境cli的配置文件和浏览器的配置文件路径不同,如mamppro.cli下运行php --ini查看

    composer安装workerman

    cd your_path/laravel_program
    composer require workerman/workerman
    artisan command实现

    因为workerman服务启动是基于cli命令行模式,所以我们得用laravel的artisan来实现.

    创建command

    以下例子是创建一个简单的httpserver.其他服务请查看官方文档.

    php artisan make:command WorkermanHttpserver
    laravel5.3改成command了,5.2 5.1 是console

    进入App\Console\Commands目录下

    1. <?php 
    2. namespace App\Console\Commands; 
    3. use Illuminate\Console\Command; 
    4. use Workerman\Worker; 
    5. use App; 
    6. class WorkermanHttpServer extends Command 
    7.  protected $httpserver
    8.  /** 
    9.  * The name and signature of the console command. 
    10.  * 
    11.  * @var string 
    12.  */ 
    13.  protected $signature = 'workerman:httpserver {action} {--daemonize}'
    14.  /** 
    15.  * The console command description. 
    16.  * 
    17.  * @var string 
    18.  */ 
    19.  protected $description = 'workerman httpserver'
    20.  /** 
    21.  * Create a new command instance. 
    22.  * 
    23.  * @return void 
    24.  */ 
    25.  public function __construct() 
    26.  { 
    27.  parent::__construct(); 
    28.  } 
    29.  /** 
    30.  * Execute the console command. 
    31.  * 
    32.  * @return mixed 
    33.  */ 
    34.  public function handle() 
    35.  { 
    36.  //因为workerman需要带参数 所以得强制修改 
    37.  global $argv
    38.  $action=$this->argument('action'); 
    39.  if(!in_array($action,['start','stop'])){ 
    40.  $this->error('Error Arguments'); 
    41.  exit
    42.  } 
    43.  $argv[0]='workerman:httpserver'
    44.  $argv[1]=$action
    45.  $argv[2]=$this->option('daemonize')?'-d':''
    46.  $this->httpserver=new Worker('http://0.0.0.0:8080'); 
    47.  // App::instance('workerman:httpserver',$this->httpserver); 
    48.  $this->httpserver->onMessage=function($connection,$data){ 
    49.  $connection->send('laravel workerman hello world'); 
    50.  }; 
    51.  Worker::runAll(); 
    52.  } 

    注册command

    App\Console\Kernel.php文件添加刚才创建的command

    protected $commands = [
    Commands\WorkermanHttpServer::class
    ];
    运行

    #debug运行
    php artisan workerman:httpserver start
    #常驻后台运行
    php artisan workerman:httpserver start --daemonize

    laravel项目整合workerman长连接

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-153-6164-1.html
    相关热词搜索: