由于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')
项目中使用服务来执行webservice,由于对方系统api不稳定,经常导致服务崩溃,只能重启,一个月差不多要重启一次。初期的解决办法是捕获异常,然后continue掉。<?php try { $url = 'http...
(1).config.php 配置文件<?php /** * RabbitMQ_Config */ $config = [ 'host' => ...
主要原理是通过PHP创建多个子进程,在子进程中发送进程闹钟信号,然后再监听闹钟信号中继续发送闹钟信号。同时通过父进程设置非阻塞运行。代码如下:<?php /** * 订单任务 */ class Order { &n...
<?php $member = new class { public function getInfo() { ...
自己的composer已经发布到packagist,但是无法使用composer require easy-task/easy-task来安装,只能在配置文件使用如下方式安装:"require": { "easy...
使用openssl扩展对应替换mcrypt的函数,(比较麻烦,但是openssl是未来趋势)在新版php中编译mcrypt扩展使用一个纯php代码实现的mcrypt扩展库,git地址为https://github.com/phpseclib/mcrypt_compat,每个mcrypt的方法都已经实...