DoraRPC\Server::onRequest PHP Method

onRequest() final public method

http request process
final public onRequest ( swoole_http_request $request, swoole_http_response $response )
$request swoole_http_request
$response swoole_http_response
    public final function onRequest(\swoole_http_request $request, \swoole_http_response $response)
    {
        //return the json
        foreach ($this->httpConfig['response_header'] as $k => $v) {
            $response->header($k, $v);
        }
        //forever http 200 ,when the error json code decide
        $response->status(200);
        //chenck post error
        if (!isset($request->post["params"])) {
            $response->end(json_encode(Packet::packFormat("Parameter was not set or wrong", 100003)));
            return;
        }
        //get the post parameter
        $params = $request->post;
        $params = json_decode($params["params"], true);
        //check the parameter need field
        if (!isset($params["guid"]) || !isset($params["api"]) || count($params["api"]) == 0) {
            $response->end(json_encode(Packet::packFormat("Parameter was not set or wrong", 100003)));
            return;
        }
        //task base info
        $task = array("guid" => $params["guid"], "fd" => $request->fd, "protocol" => "http");
        $url = trim($request->server["request_uri"], "\r\n/ ");
        switch ($url) {
            case "api/multisync":
                $task["type"] = DoraConst::SW_MODE_WAITRESULT_MULTI;
                foreach ($params["api"] as $k => $v) {
                    $task["api"] = $params["api"][$k];
                    $taskid = $this->server->task($task, -1, function ($serv, $task_id, $data) use($response) {
                        $this->onHttpFinished($serv, $task_id, $data, $response);
                    });
                    $this->taskInfo[$task["fd"]][$task["guid"]]["taskkey"][$taskid] = $k;
                }
                break;
            case "api/multinoresult":
                $task["type"] = DoraConst::SW_MODE_NORESULT_MULTI;
                foreach ($params["api"] as $k => $v) {
                    $task["api"] = $params["api"][$k];
                    $this->server->task($task);
                }
                $pack = Packet::packFormat("transfer success.已经成功投递", 100001);
                $pack["guid"] = $task["guid"];
                $response->end(json_encode($pack));
                break;
            case "server/cmd":
                $task["type"] = DoraConst::SW_CONTROL_CMD;
                if ($params["api"]["cmd"]["name"] == "getStat") {
                    $pack = Packet::packFormat("OK", 0, array("server" => $this->server->stats()));
                    $pack["guid"] = $task["guid"];
                    $response->end(json_encode($pack));
                    return;
                }
                if ($params["api"]["cmd"]["name"] == "reloadTask") {
                    $pack = Packet::packFormat("OK", 0, array('server' => $this->server->stats()));
                    $this->server->reload(true);
                    $pack["guid"] = $task["guid"];
                    $response->end(json_encode($pack));
                    return;
                }
                break;
            default:
                $response->end(json_encode(Packet::packFormat("unknow task type.未知类型任务", 100002)));
                unset($this->taskInfo[$task["fd"]]);
                return;
        }
    }