workerman结合thinkphp

xiaoxiao2021-02-28  79

这两天开始对workerman进行研究,怎样将workerman和tp结合起来用命令启动,又不用进入很深的目录,终于有了解决方案。 首先说明这是基于linux系统开发的 其实很简单,下载workerman,当作模块放在Application中。 在Controller中引入workerman使用:use Workerman\Worker; 载入:require_once APP_PATH . 'Workerman/Autoloader.php';  服务端启动代码: public function index() { $http_worker = new Worker("http://0.0.0.0:2345"); // 启动4个进程对外提供服务 $http_worker->count = 4; // 接收到浏览器发送的数据时回复hello world给浏览器 $http_worker->onMessage = function ($connection, $data) { // 向浏览器发送hello world $connection->send('hello world'); }; // 运行worker Worker::runAll(); } 重点来了:修改Workerman的Worker.php第586行为以下代码 protected static function parseCommand() { global $argv; // Check argv; // print_r($argv); $start_file = $argv[0]; if (!isset($argv[2])) { exit("Usage: php yourfile.php Controller/Action {start|stop|restart|reload|status}\n"); } // Get command. $command = trim($argv[2]); $command2 = isset($argv[3]) ? $argv[3] : ''; ....... 说明:既然以命令模式(cli)运行(注意与 fpm 的区别,后者处理来自网页端的请求),就必然有一个启动脚本解析命令,譬如说3.x版本(之前默认为daemon)新增一个 -d 参数,以表示守护进程运行,解析到该参数设置 self::$daemon = true, 随后fork子进程以脱离当前进程组,设置进程组组长等工作。这里有两个非常重要的参数 $argc 和 $argc,前者表示参数个数,后者为一个数组,保存有命令的所有参数,比如:sudo php start.php start -d,$argv就是 array( [0]=>start.php, [1]=>start, [2]=>-d ),而解析主要用到$argv。 最后:进入index.php根目录命令运行

php index.php Home/Index/index start -d

更过知识

转载请注明原文地址: https://www.6miu.com/read-64941.html

最新回复(0)