参考了其他人的解决方案,按如下来做:
在Gateway的Application下新增了一个Worker来处理繁重任务(要调外部的http接口)
> $task_worker = new Worker('Text://127.0.0.1:13000');
> $task_worker->count = 10;
> $task_worker->name = 'TaskWorker';
> $task_worker->onMessage = function($connection, $task_data)
> {
> $task_data = json_decode($task_data, true);
> $rs = Transfer::start($task_data, $task_data);
> $connection->send(json_encode($rs));
> };
然后,在Event里处理发过来的请求消息,发给上面的Worker异步处理
> $task_connection = new AsyncTcpConnection('Text://127.0.0.1:13000');
> $task_data = array(
> 'func' => $req_data,
> 'data' => $req_data
> );
> $task_connection->send(json_encode($task_data));
> $task_connection->onMessage = function($task_connection, $task_result)use($client_id)
> {
> Gateway::sendToClient($client_id, json_encode(array("flag" => "transfer", "msg" => $task_result)));
> $task_connection->close();
> };
> $task_connection->connect();
测试后,功能正常,不知道这样做是否最优?
已经是很好的方案了