由于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  ...
PHP不像net支持多继承,自身只支持单继承,为了解决这个问题,php出了Trait这个特性,减少单继承语言的限制。并且能让代码复用率更高。说白了就是一个对象的属性和方法扩展工具一样。例如:trait exts { public f...
第一次听说端口复用是在mixphp最新版本中发现的,mixphp启动监听9501端口,现在作者说可以多开几个进程来执行mixphp,我心里想了下再启动不是会端口冲突嘛,但是却没有问题,于是下载mixphp的源码解读,原来是启动http服务器使用new Co\Http\Server('0.0....
最近在项目中处理一个关于商品数据重复需要删除多余的商品记录,但是删除一条商品必然要把关联的其他表商品的id和其他商品信息更换为正确的,删除一个商品记录,同时要去修改100多张表的关联商品数据,在项目中引用了tp orm 1.2版本,由于项目是php5.6版本,没法使用最新orm,在代码中每处理1个商...
PHP简单定时器可以通过pcntl_signal创建闹钟信号来实现。但是缺点很明显,性能一般,要自己实现守护进程,不支持毫秒级定时器,单进程不支持多个闹钟信号,不能跨平台运行event扩展支持的事件多,性能高。<?php //创建event配置.[空配置] $eventConfig ...
【一】.基本用法首先看看官方对send方法的解释:Sets the return value of the yield expression and resumes the generato...