我現(xiàn)在是用命令行啟動(dòng)了http服務(wù),并在onmessage中執(zhí)行tp的http,但是一直訪問(wèn)到控制器index下面的index方法,其他api更換路由也無(wú)法訪問(wèn)。
<?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');
// 創(chuàng)建一個(gè)Worker監(jiān)聽(tīng)2345端口,使用http協(xié)議通訊
$http_worker = new Worker("http://0.0.0.0:2345");
// 啟動(dòng)4個(gè)進(jìn)程對(duì)外提供服務(wù)
$http_worker->count = 4;
// 接收到瀏覽器發(fā)送的數(shù)據(jù)時(shí)回復(fù)hello world給瀏覽器
$http_worker->onMessage = function (TcpConnection $connection, Request $request) {
// // 向?yàn)g覽器發(fā)送hello world
// $connection->send('hello world');
// 執(zhí)行HTTP應(yīng)用并響應(yīng)
$http = (new App())->http;
$response = $http->run();
$response->send();
$http->end($response);
$connection->send(new Response($response->getCode(), $response->getHeader(), $response->getContent()));
};
// 運(yùn)行worker
Worker::runAll();
}
}
用上面的代碼啟動(dòng)后無(wú)論我怎么更改訪問(wèn)路徑始終只會(huì)輸出index/index方法
因?yàn)槟愕膖hinkphp里面的request實(shí)例獲取不到當(dāng)前worker的請(qǐng)求信息,也就不認(rèn)識(shí)你的index應(yīng)用和index方法,需要自己做一下適配,過(guò)程稍微有點(diǎn)復(fù)雜,這里不再過(guò)多贅述····