当前位置:首页 > PHP > 正文内容

thinkphp5整合workerman,tp5整合workerman

高老师6年前 (2020-04-17)PHP363837

由于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')

扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/131.html

分享给朋友:

“thinkphp5整合workerman,tp5整合workerman” 的相关文章

cookie中文乱码,GBK下

cookie中文乱码,GBK下

上家公司开发医院挂号系统,系统采用GBK编码。ajax发送的中文用户名让PHP保存为cookie出现乱码的解决方案。1.Javascript变量var user=document.getElementById('user').innerText; user=escape(u...

php 开启错误提示,php 关闭错误提示

php 开启错误提示,php 关闭错误提示

开启错误提示代码:ini_set("display_errors", "On"); error_reporting(E_ALL | E_STRICT);关闭错误提示代码:error_reporting(E_ALL ^&n...

PHP二维数组排序,PHP多维数组排序, array_multisort()

PHP二维数组排序,PHP多维数组排序, array_multisort()

使用php函数array_multisort()即可实现和SQL一样的order by排序. 例如我们需要对会员表按照主键降序排列,年龄升序排列://会员表数据 $list = []; $list[] = ['mid' =>&n...

xmlrpc  php,php通过xml-rpc进行通信

xmlrpc php,php通过xml-rpc进行通信

xmlrpc协议是通过http请求xml数据进行通信。webservice中和它相同的是soap。soap调用的确很简单,但是创建wsdl太繁琐,效率低下。xmlrpc很好的解决这个问题。(1).创建xmlrpc服务端(求和函数api)function getSum($method,$ar...

php限制方法返回值类型

php限制方法返回值类型

php7新增的特性(1).强制限制只能返回一种类型<?php class task { } //must return an integer function add(): int {    &nb...

php redis事务

php redis事务

概念请参考w3school文章: redis watch ,redis exec (看完基本秒懂)(1)基本事务://连接本地的 Redis 服务 $redis = new Redis(); $redis->con...