您现在的位置是:首页 > 博客日记 > Php Php

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

2020-04-20 12:36:55 【Php】 人已围观

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

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

  1. /**
  2. * "onRequest" listener.
  3. *
  4. * @param Request $req
  5. * @param Response $res
  6. */
  7. public function onRequest($req, $res)
  8. {
  9. /**
  10. * TinyMeng update start
  11. */
  12. $httpRequest = config("swoole.websocket.httpRequest");
  13. if(!empty($httpRequest)){
  14. if(isset($req->get['websocket'])){
  15. $websocket = new $httpRequest($this->getServer());
  16. return $websocket->onRequest($req,$res);
  17. }else{
  18. //return true;
  19. }
  20. }
  21. /** Tinymeng end */
  22. $args = func_get_args();
  23. $this->runInSandbox(function (Http $http, Event $event, App $app, Middleware $middleware) use ($args, $req, $res) {
  24. $event->trigger('swoole.request', $args);
  25. //兼容var-dumper
  26. if (class_exists(VarDumper::class)) {
  27. $middleware->add(ResetVarDumper::class);
  28. }
  29. $request = $this->prepareRequest($req);
  30. try {
  31. $response = $this->handleRequest($http, $request);
  32. } catch (Throwable $e) {
  33. $response = $this->app
  34. ->make(Handle::class)
  35. ->render($request, $e);
  36. }
  37. $this->sendResponse($res, $response, $app->cookie);
  38. });
  39. }


关注TinyMeng博客,更多精彩分享,敬请期待!
 

很赞哦! ()