0

先贴一段代码

class Server
{
    public $server;
    public $redisPool = [];
    public function run()
    {
        $this->server = new Swoole\Http\Server("0.0.0.0", 9501, SWOOLE_BASE);
        $this->server->set([
            'worker_num' => 1,
        ]);
        $this->server->on('Connect', [$this, 'onConnect']);
        $this->server->on('Request', [$this, 'onRequest']);
        $this->server->on('Close', [$this, 'onClose']);
        $this->server->start();
    }
    public function onConnect($serv, $fd, $reactorId)
    {
    }
    public function onClose($serv, $fd, $reactorId)
    {
    }
    public function onRequest($request, $response)
    {
        $obj = new M();
        $rs = $obj->todo();
        var_dump($rs);
        $response->end('xxxx');
    }
}
$server = new Server();
$server->run();

class M{
    function todo()
    {
        $swoole_mysql = new Swoole\Coroutine\MySQL();
        $swoole_mysql->connect(['host' => '127.0.0.1', 'user' => 'root', 'password' => 'hello123', 'database' => 'putao_admin']);
        $res = $swoole_mysql->query('select sleep(1)');
        return $res;
    }
}

执行后浏览器访问http://127.0.0.1:9501,页面会一直loading

我看到官方文档有说"协程组件只能在服务器的onConnect、onRequest、onReceive、onMessage 回调函数中使用"
我想问的是,难道我封装个有协程组件的类,然后去调用都不行吗? 是必须在这些服务器回调函数中使用吗?

这个问题已被关闭,原因:问题已解决 - 问题已解决,且对他人无借鉴意义

2017-01-03 提问
0 个回答

这个问题已经被关闭无法回答

推广链接