由于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')
在项目中需要对图片进行裁剪,前端裁剪完成发送base64给后端,但是很意外的PHP获取到的数据和前端有点差距,之前我都是先加密,后端解密,但是这次依然不行。于是使用filter_input方法轻松解决。$base64 = filter_input(INPUT_POST...
开启错误提示代码:ini_set("display_errors", "On"); error_reporting(E_ALL | E_STRICT);关闭错误提示代码:error_reporting(E_ALL ^&n...
第一步:服务端文件<?php $wsdlfile='webservice.wsdl'; ini_set('soap.wsdl_cache_enabled','0'); //关闭WSDL缓存 //001...
ThinkPHP中有一个debug调试功能,能输出报错文件的信息,并能看到这个函数被哪些函数调用,从框架的启动开始记录,特别方便调试。于是研究了下它的底层给予了实现。<?php //--框架核心--Start //框架内置错误处理 function errDealWith($er...
<?php /** * daemonize让当前脚本为守护进程执行 * @param string $callback 匿名函数 */ function daemonize($callback) {...
当我想在一个进程中监听kill 和 kill -9命令报了这个错误。//监听kill pcntl_signal(SIGTERM, function () { posix_kill(0, SIGTERM); });...