由于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')
<?php //对比$this和self /* * $this更倾向于对象本身 * */ class Par{ public  ...
上篇文章已经讲解arrayacces的原理,现在来讲解下arrayaccess的实际应用。一个大型的互联网项目中必然会存在各种配置信息,例如多种数据库信息:mysql,tidb,mongodb,redis,某个业务模块单独的配置信息如比例,额度等等,那么该如何治理配置信息?PHP项目中大部分的框架都...
ThinkPHP中有一个debug调试功能,能输出报错文件的信息,并能看到这个函数被哪些函数调用,从框架的启动开始记录,特别方便调试。于是研究了下它的底层给予了实现。<?php //--框架核心--Start //框架内置错误处理 function errDealWith($er...
<?php /** * @throws Exception */ function curl() { throw new \Exception('err...
最近在编写windows php多线程的东西,从官网下载了PHP的线程安全版,尝试开启curl扩展extension=php_curl.dllphp -m 却提示 PHP Startup: Unable to load dynamic library 'C:\php\ext\php_curl...
有时候我们需要爬一个接口,但是这个接口需要很多参数,包括header和cookie要去编写,使用php curl模拟实在太慢。我们可以通过浏览器的network来复制请求为curl命令。例如我需要模拟请求接口地址:https://www.xkmz.cc/Ajax/Debug/delly,我们只需要在...