配置
首先运行命令检测当前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目录下
<?php
-
namespace App\Console\Commands;
-
use Illuminate\Console\Command;
use Workerman\Worker;
use App;
class WorkermanHttpServer extends Command
{
protected $httpserver;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'workerman:httpserver {action} {--daemonize}';
-
/**
* The console command description.
*
* @var string
*/
protected $description = 'workerman httpserver';
-
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
-
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//因为workerman需要带参数 所以得强制修改
global $argv;
$action=$this->argument('action');
-
if(!in_array($action,['start','stop'])){
$this->error('Error Arguments');
exit;
}
-
$argv[0]='workerman:httpserver';
$argv[1]=$action;
$argv[2]=$this->option('daemonize')?'-d':'';
-
$this->httpserver=new Worker('http://0.0.0.0:8080');
-
// App::instance('workerman:httpserver',$this->httpserver);
-
$this->httpserver->onMessage=function($connection,$data){
$connection->send('laravel workerman hello world');
};
-
Worker::runAll();
}
}
注册command
App\Console\Kernel.php
文件添加刚才创建的command
protected $commands = [
Commands\WorkermanHttpServer::class
];
运行
#debug运行
php artisan workerman:httpserver start
#常驻后台运行
php artisan workerman:httpserver start --daemonize
转至:
https://www.bandari.net/blog/19