Skip to content

Commit 5aa9280

Browse files
committed
fix获取flash后自动删除session,新增feehi/console/dump输出调试函数,增加更多swoole server配置项
1 parent f222b21 commit 5aa9280

File tree

5 files changed

+80
-144
lines changed

5 files changed

+80
-144
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.idea
2+
.DS_Store

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ Nginx配置
149149
}
150150
```
151151

152+
153+
调试
154+
-------------
155+
var_dump、echo都是输出到控制台,不方便调试。可以使用\feehi\console\dump()函数,输出数组、对象、字符串、布尔值到浏览器
156+
157+
152158
其他
153159
-------------
154160
以上是把swoole启动/关闭/重启命令集成到了yii2 console里面,如果你并不想使用集成到yii2 console的swoole,可以复制vendor/feehi/yii2-swoole下的backend.php和frontend.php,修改$rootDir = "/path/to/project"为真正的yii2项目根目录,然后执行php backend.php以及php frontend.php启动swoole

src/console/SwooleController.php

Lines changed: 33 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -9,109 +9,13 @@
99
namespace feehi\console;
1010

1111
use yii;
12-
13-
/**
14-
* Class SwooleController
15-
*
16-
* @package feehi\console\controllers
17-
*
18-
* @description
19-
*
20-
* 支持的命令
21-
*
22-
* ./yii swoole/start 启动前台swoole
23-
* ./yii swoole/stop 关闭前台swoole
24-
* ./yii swoole/restart 重启前台swoole
25-
*
26-
* ./yii swoole-backend/start 启动后台swoole
27-
* ./yii swoole-backend/stop 关闭后台swoole
28-
* ./yii swoole-backend/restart 重启后台swoole
29-
*
30-
*
31-
* 配置示例
32-
'controllerMap'=>[
33-
...
34-
'swoole' => [
35-
'class' => feehi\console\SwooleController::className(),
36-
'rootDir' => str_replace('console/config', '', __DIR__ ),//yii2项目根路径
37-
'app' => 'frontend',//app目录地址
38-
'host' => '127.0.0.1',//监听地址
39-
'port' => 9999,//监听端口
40-
'swooleConfig' => [//标准的swoole配置项都可以再此加入
41-
'reactor_num' => 2,
42-
'worker_num' => 4,
43-
'daemonize' => false,
44-
'log_file' => __DIR__ . '/../../frontend/runtime/logs/swoole.log',
45-
'log_level' => 0,
46-
'pid_file' => __DIR__ . '/../../frontend/runtime/server.pid',
47-
],
48-
],
49-
'swoole-backend' => [
50-
'class' => feehi\console\SwooleController::className(),
51-
'rootDir' => str_replace('console/config', '', __DIR__ ),//yii2项目根路径
52-
'app' => 'backend',
53-
'host' => '127.0.0.1',
54-
'port' => 9998,
55-
'swooleConfig' => [
56-
'reactor_num' => 2,
57-
'worker_num' => 4,
58-
'daemonize' => false,
59-
'log_file' => __DIR__ . '/../../backend/runtime/logs/swoole.log',
60-
'log_level' => 0,
61-
'pid_file' => __DIR__ . '/../../backend/runtime/server.pid',
62-
],
63-
]
64-
...
65-
]
66-
*
67-
* nginx 配置示列 //虽然swoole从1.9.8开始支持静态资源,但是性能很差,线上环境务必搭配nginx使用
68-
*
69-
* 前台
70-
*
71-
server {
72-
set $web /www/cms-swoole/frontend/web;
73-
root $web;
74-
server_name swoole.cms.test.docker;
75-
76-
location ~* .(ico|gif|bmp|jpg|jpeg|png|swf|js|css|mp3) {
77-
root $web;
78-
}
79-
80-
location ~ timthumb\.php$ {//若部分功能仍需要使用php-fpm则做类似配置,否则删除此段
81-
fastcgi_pass 127.0.0.1:9000;
82-
fastcgi_index index.php;
83-
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
84-
include fastcgi_params;
85-
}
86-
87-
location / {
88-
proxy_http_version 1.1;
89-
proxy_set_header Connection "keep-alive";
90-
proxy_set_header X-Real-IP $remote_addr;
91-
proxy_set_header Host http://swoole.cms.test.docker;
92-
proxy_pass http://127.0.0.1:9999;
93-
}
94-
}
95-
*
96-
后台
97-
server {
98-
set $web /www/cms-swoole/backend/web;
99-
root $web;
100-
server_name swoole-admin.cms.test.docker;
101-
102-
location ~* .(ico|gif|bmp|jpg|jpeg|png|swf|js|css|mp3) {
103-
root $web;
104-
}
105-
106-
location / {
107-
proxy_http_version 1.1;
108-
proxy_set_header Connection "keep-alive";
109-
proxy_set_header X-Real-IP $remote_addr;
110-
proxy_set_header Host http://swoole-admin.cms.test.docker;
111-
proxy_pass http://127.0.0.1:9998;
112-
}
113-
}
114-
*/
12+
use yii\helpers\ArrayHelper;
13+
use feehi\web\Request;
14+
use feehi\web\Response;
15+
use feehi\web\Session;
16+
use feehi\swoole\SwooleServer;
17+
use yii\web\AssetManager;
18+
use yii\web\Application;
11519

11620
class SwooleController extends \yii\console\Controller
11721
{
@@ -120,12 +24,18 @@ class SwooleController extends \yii\console\Controller
12024

12125
public $port = 9999;
12226

27+
public $mode = SWOOLE_PROCESS;
28+
29+
public $socketType = SWOOLE_TCP;
30+
12331
public $rootDir = "";
12432

12533
public $app = "frontend";
12634

12735
public $swooleConfig = [];
12836

37+
public $gcSessionInterval = 60000;//启动session回收的间隔时间,单位为毫秒
38+
12939

13040

13141
public function actionStart()
@@ -142,7 +52,7 @@ public function actionStart()
14252
require($rootDir . '/common/config/bootstrap.php');
14353
require($rootDir . $this->app . '/config/bootstrap.php');
14454

145-
$config = yii\helpers\ArrayHelper::merge(
55+
$config = ArrayHelper::merge(
14656
require($rootDir . '/common/config/main.php'),
14757
require($rootDir . '/common/config/main-local.php'),
14858
require($rootDir . $this->app . '/config/main.php'),
@@ -154,54 +64,55 @@ public function actionStart()
15464
'enable_static_handler' => true,
15565
], $this->swooleConfig);
15666

157-
$server = new \feehi\swoole\SwooleServer($this->host, $this->port, $this->swooleConfig);
67+
$server = new SwooleServer($this->host, $this->port, $this->mode, $this->socketType, $this->swooleConfig, ['gcSessionInterval'=>$this->gcSessionInterval]);
68+
69+
function dump($var){
70+
if( is_array($var) || is_object($var) ){
71+
$body = print_r($var, true);
72+
}else{
73+
$body = $var;
74+
}
75+
if( isset(yii::$app->getResponse()->swooleResponse) ){
76+
echo "dump function must called in request period" . PHP_EOL;
77+
}
78+
yii::$app->getResponse()->swooleResponse->end($body);
79+
}
15880

15981
/**
16082
* @param \swoole_http_request $request
16183
* @param \swoole_http_response $response
16284
*/
16385
$server->runApp = function ($request, $response) use ($config, $web) {
164-
/*$uri = $request->server['request_uri'];
165-
if (strpos($uri, 'timthumb')) {
166-
$image = new \feehi\components\PicFilter();
167-
$image->initialize([
168-
'source_image' => $web . "/uploads/article/thumb/5998ec3c119ea_a6.jpg",
169-
'width' => 200,
170-
'height' => 200,
171-
]);
172-
$image->resize();
173-
exit;
174-
}*/
17586
$aliases = [
17687
'@web' => $web,
17788
'@webroot' => $web,
17889
];
17990
$config['aliases'] = isset($config['aliases']) ? array_merge($aliases, $config['aliases']) : $aliases;
18091

18192
$requestComponent = [
182-
'class' => \feehi\web\Request::className(),
93+
'class' => Request::className(),
18394
'swooleRequest' => $request,
18495
];
18596
$config['components']['request'] = isset($config['components']['request']) ? array_merge($config['components']['request'], $requestComponent) : $requestComponent;
18697

18798
$responseComponent = [
188-
'class' => \feehi\web\Response::className(),
99+
'class' => Response::className(),
189100
'swooleResponse' => $response,
190101
];
191102
$config['components']['response'] = isset($config['components']['response']) ? array_merge($config['components']['response'], $responseComponent) : $responseComponent;
192103

193104
$authManagerComponent = [
194-
'class' => yii\web\AssetManager::className(),
105+
'class' => AssetManager::className(),
195106
'baseUrl' => '/assets'
196107
];
197108
$config['components']['assetManager'] = isset( $config['components']['assetManager'] ) ? array_merge($authManagerComponent, $config['components']['assetManager']) : $authManagerComponent;
198109

199110
$config['components']['session'] = [
200-
"class" => \feehi\web\Session::className()
111+
"class" => Session::className()
201112
];
202113

203114
try {
204-
$application = new \yii\web\Application($config);
115+
$application = new Application($config);
205116
yii::setAlias('@web', $web);
206117
yii::$app->setAliases($aliases);
207118
$application->run();
@@ -251,7 +162,7 @@ private function sendSignal($sig)
251162
if ($pid = $this->getPid()) {
252163
posix_kill($pid, $sig);
253164
} else {
254-
$this->stdout("not running!" . PHP_EOL);
165+
$this->stdout("server is not running!" . PHP_EOL);
255166
exit(1);
256167
}
257168
}

src/swoole/SwooleServer.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,23 @@
99
namespace feehi\swoole;
1010

1111
use feehi\web\Session;
12+
use swoole_http_server;
1213

1314
class SwooleServer extends \yii\base\Object
1415
{
1516
public $swoole;
1617

17-
public static $swooleConfig;
18+
public $webRoot;
19+
20+
public $config = ['gcSessionInterval' => 60000];
1821

1922
public $runApp;
2023

21-
public function __construct($host, $port, $swooleConfig=[])
24+
public function __construct($host, $port, $mode, $socketType, $swooleConfig=[], $config=[])
2225
{
23-
$this->swoole = new \swoole_http_server($host, $port);
24-
self::$swooleConfig = $swooleConfig;
26+
$this->swoole = new swoole_http_server($host, $port, $mode, $socketType);
27+
$this->webRoot = $swooleConfig['document_root'];
28+
if( !empty($this->config) ) $this->config = array_merge($this->config, $config);
2529
$this->swoole->set($swooleConfig);
2630
$this->swoole->on('request', [$this, 'onRequest']);
2731
$this->swoole->on('WorkerStart', [$this, 'onWorkerStart']);
@@ -53,7 +57,7 @@ public function onRequest($request, $response)
5357

5458
public function onWorkerStart( $serv , $worker_id) {
5559
if( $worker_id == 0 ) {
56-
\swoole_timer_tick(60000, function(){//一分钟清理一次session
60+
swoole_timer_tick($this->config['gcSessionInterval'], function(){//一分钟清理一次session
5761
(new Session())->gcSession();
5862
});
5963
}
@@ -84,19 +88,19 @@ private function staticRequest($request, $response)
8488
$extension = pathinfo($uri, PATHINFO_EXTENSION);
8589
if( !empty($extension) && in_array($extension, ['js', 'css', 'jpg', 'jpeg', 'png', 'gif', 'webp']) ){
8690

87-
$web = self::$swooleConfig['document_root'];
91+
$web = $this->webRoot;
8892
rtrim($web, '/');
8993
$file = $web . '/' . $uri;
9094
if( is_file( $file )){
9195
$temp = strrev($file);
9296
if( strpos($uri, 'sj.') === 0 ) {
93-
$response->header('Content-Type', 'application/x-javascript');
97+
$response->header('Content-Type', 'application/x-javascript', false);
9498
}else if(strpos($temp, 'ssc.') === 0){
95-
$response->header('Content-Type', 'text/css');
99+
$response->header('Content-Type', 'text/css', false);
96100
}else {
97-
$response->header('Content-Type', 'application/octet-stream');
101+
$response->header('Content-Type', 'application/octet-stream', false);
98102
}
99-
$response->sendfile($file);
103+
$response->sendfile($file, 0);
100104
}else{
101105
$response->status(404);
102106
$response->end('');

0 commit comments

Comments
 (0)