大佬们怎么用thinkphp8整合workerman的http服务来运行api?

张大娃

问题描述

我现在是用命令行启动了http服务,并在onmessage中执行tp的http,但是一直访问到控制器index下面的index方法,其他api更换路由也无法访问。

<?php

declare(strict_types=1);

namespace app\command;

use think\App;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use Workerman\Protocols\Http\Response;
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;

class WorkerServer extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('workerServer')
            ->setDescription('the workerServer command');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        // $output->writeln('workerServer');
        // 创建一个Worker监听2345端口,使用http协议通讯
        $http_worker = new Worker("http://0.0.0.0:2345");

        // 启动4个进程对外提供服务
        $http_worker->count = 4;

        // 接收到浏览器发送的数据时回复hello world给浏览器
        $http_worker->onMessage = function (TcpConnection $connection, Request $request) {
            // // 向浏览器发送hello world
            // $connection->send('hello world');
            // 执行HTTP应用并响应
            $http = (new App())->http;

            $response = $http->run();

            $response->send();

            $http->end($response);

            $connection->send(new Response($response->getCode(), $response->getHeader(), $response->getContent()));
        };

        // 运行worker
        Worker::runAll();
    }
}

用上面的代码启动后无论我怎么更改访问路径始终只会输出index/index方法
截图

242 1 0
1个回答

释永战

因为你的thinkphp里面的request实例获取不到当前worker的请求信息,也就不认识你的index应用和index方法,需要自己做一下适配,过程稍微有点复杂,这里不再过多赘述····

  • 暂无评论
×
🔝