From 1d58446245b04ada2b720241e31284e3f3366386 Mon Sep 17 00:00:00 2001 From: koogua Date: Sun, 3 Oct 2021 09:47:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3websocket=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Api/Controllers/Controller.php | 17 +++++++++++++++++ app/Http/Home/Controllers/Controller.php | 2 +- app/Validators/Validator.php | 14 ++++++++++++++ config/config.default.php | 2 +- 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/Http/Api/Controllers/Controller.php b/app/Http/Api/Controllers/Controller.php index 60c3dac3..c2f85e17 100644 --- a/app/Http/Api/Controllers/Controller.php +++ b/app/Http/Api/Controllers/Controller.php @@ -11,6 +11,7 @@ use App\Models\User as UserModel; use App\Services\Auth\Api as ApiAuth; use App\Traits\Response as ResponseTrait; use App\Traits\Security as SecurityTrait; +use App\Validators\Validator as AppValidator; use Phalcon\Mvc\Dispatcher; class Controller extends \Phalcon\Mvc\Controller @@ -30,6 +31,10 @@ class Controller extends \Phalcon\Mvc\Controller $this->setCors(); } + $validator = new AppValidator(); + + $validator->checkSiteStatus(); + $this->checkRateLimit(); return true; @@ -52,4 +57,16 @@ class Controller extends \Phalcon\Mvc\Controller return $auth->getCurrentUser(); } + protected function getSiteInfo() + { + return $this->getSettings('site'); + } + + protected function getSettings($section) + { + $appService = new AppService(); + + return $appService->getSettings($section); + } + } diff --git a/app/Http/Home/Controllers/Controller.php b/app/Http/Home/Controllers/Controller.php index f295e6c8..96d1a664 100644 --- a/app/Http/Home/Controllers/Controller.php +++ b/app/Http/Home/Controllers/Controller.php @@ -147,7 +147,7 @@ class Controller extends \Phalcon\Mvc\Controller * ssl通过nginx转发实现 */ if ($this->request->isSecure()) { - $websocket->connect_url = sprintf('wss://%s/wss', $this->request->getHttpHost()); + $websocket->connect_url = sprintf('wss://%s/wss', $websocket->connect_address); } else { $websocket->connect_url = sprintf('ws://%s', $websocket->connect_address); } diff --git a/app/Validators/Validator.php b/app/Validators/Validator.php index ea2d7ad5..d395a851 100644 --- a/app/Validators/Validator.php +++ b/app/Validators/Validator.php @@ -8,12 +8,26 @@ namespace App\Validators; use App\Exceptions\Forbidden as ForbiddenException; +use App\Exceptions\ServiceUnavailable; +use App\Exceptions\ServiceUnavailable as ServiceUnavailableException; use App\Exceptions\Unauthorized as UnauthorizedException; +use App\Services\Service as AppService; use Phalcon\Di\Injectable; class Validator extends Injectable { + public function checkSiteStatus() + { + $service = new AppService(); + + $siteInfo = $service->getSettings('site'); + + if ($siteInfo['status'] == 'closed') { + throw new ServiceUnavailableException('sys.service_unavailable'); + } + } + public function checkAuthUser($userId) { if (empty($userId)) { diff --git a/config/config.default.php b/config/config.default.php index 889f30da..2a1e5a53 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -150,7 +150,7 @@ $config['throttle']['rate_limit'] = 60; /** * 客户端ping服务端间隔(秒) */ -$config['websocket']['ping_interval'] = 50; +$config['websocket']['ping_interval'] = 30; /** * 客户端连接地址(外部可访问的域名或ip),带端口号