用thinkphp6框架的think-swoole实现websocket的onRequest回调事件

修改thinkphp6的底层类vendor/topthink/think-swoole/src/concerns/InteractsWithHttp.php第96行

    /**
     * "onRequest" listener.
     *
     * @param Request $req
     * @param Response $res
     */
    public function onRequest($req, $res)
    {
        /**
         * TinyMeng update start
         */
        $httpRequest = config("swoole.websocket.httpRequest");
        if(!empty($httpRequest)){
            if(isset($req->get['websocket'])){
                $websocket = new $httpRequest($this->getServer());
                return $websocket->onRequest($req,$res);
            }else{
                //return true;
            }
        }
        /** Tinymeng end */

        $args = func_get_args();
        $this->runInSandbox(function (Http $http, Event $event, App $app, Middleware $middleware) use ($args, $req, $res) {
            $event->trigger('swoole.request', $args);

            //兼容var-dumper
            if (class_exists(VarDumper::class)) {
                $middleware->add(ResetVarDumper::class);
            }

            $request = $this->prepareRequest($req);
            try {
                $response = $this->handleRequest($http, $request);
            } catch (Throwable $e) {
                $response = $this->app
                    ->make(Handle::class)
                    ->render($request, $e);
            }

            $this->sendResponse($res, $response, $app->cookie);
        });
    }
Last modification:June 28th, 2020 at 10:16 am
如果觉得我的文章对你有用,请随意赞赏