由于workerman底层直接读取$_SERVER['argv']的命令行参数,没有提供独立的方法start/stop,而tp的命令行参数无法适配workerman,虽然thinkphp官方专门做了一个适配的版本,但是看了下评论问题挺多的。于是自己来搞一个.
(1).在application/command.php中添加如下代码:
return [ 'app\socket\command\Socket' ];
(2).创建 application/socket/command目录,在这个目录创建Socket.php文件
<?php namespace app\socket\command; use think\console\Command; use think\console\Input; use think\console\input\Argument; use think\console\input\Option; use think\console\Output; use Workerman\Worker; class Socket extends Command { /** * 命令Input配置 */ protected function configure() { $this->setName('socket') ->addArgument('action', Argument::OPTIONAL, "action") ->addOption('other', '-d', Option::VALUE_OPTIONAL, 'test'); } /** * 重置Cli参数 */ protected function resetCli() { global $argv, $argc; $file = "{$argv['0']} {$argv['1']}"; $action = $argv['2']; $extend = empty($argv['3']) ? '' : $argv['3']; $argv = []; $argv[] = $file; $argv[] = $action; if ($extend) { $argv[] = $extend; } $argc = count($argv); $_SERVER['argv'] = $argv; $_SERVER['argc'] = $argc; } /** * 命令响应 * @param Input $input * @param Output $output * @return int|void|null */ protected function execute(Input $input, Output $output) { //01.重置Cli命令行参数 $this->resetCli(); //02.开始WorkMan代码 $ws_worker = new Worker(config('socket.socket_name')); // 启动4个进程对外提供服务 $ws_worker->count = 2; // 接收到浏览器发送的数据时回复hello world给浏览器 $ws_worker->onMessage = function ($connection, $data) { // 向浏览器发送hello world $connection->send('hello ' . $data); }; // 运行worker Worker::runAll(); } }
(3).在tp根目录执行命令
php think socket start
名字不想叫socket,可以修改$this->setName('socket')
代码1:for循环批量插入100W数据<?php set_time_limit(0); $servername = "localhost"; $username = "root"; $password ...
开启错误提示代码:ini_set("display_errors", "On"); error_reporting(E_ALL | E_STRICT);关闭错误提示代码:error_reporting(E_ALL ^&n...
首先看看以下代码:代码1:<?php $a=0.1; $b=0.7; if($a+$b==0.8) { echo "1"; } else{ echo "2"; } ?>代码2:<?php &n...
php多进程应用场景主要是非web端,fpm下是不支持多进程的,非类linux操作系统都不支持,请在cli模式使用.可以使用多进程做任务分发,批量计算,批量文件处理,批量爬虫,网络运维等等。下面看一份简单的入门demo//创建子进程 $pid=pcntl_fork(); //返回-1,创建失败,不...
逛公众号文章看到文章"php实现事件监听与触发的方法,你用过吗?",我就好奇了,php又不是asp.net的webform,哪里来的服务端事件监听。于是学习了一波。先看下监听类:class Event { /** &nbs...
重构框架的时候想要考虑支持下cli模式,于是参考了thinkphp的底层。/** * 获取应用根目录 * @return string */ public static function getRootP...