Skip to content

Commit

Permalink
add swoole session handle
Browse files Browse the repository at this point in the history
  • Loading branch information
liufee committed Aug 23, 2017
1 parent e365c1f commit 89cf5b2
Show file tree
Hide file tree
Showing 5 changed files with 1,122 additions and 70 deletions.
18 changes: 12 additions & 6 deletions src/console/SwooleController.php
Expand Up @@ -178,21 +178,27 @@ public function actionStart()
];
$config['aliases'] = isset($config['aliases']) ? array_merge($aliases, $config['aliases']) : $aliases;

$config['components']['request'] = [
$requestComponent = [
'class' => \feehi\web\Request::className(),
'swooleRequest' => $request,
'cookieValidationKey' => 'KaNMPF6oZegCr0bhED4JHYnhOse7UhrS',
'enableCsrfValidation' => true,
];
$config['components']['response'] = [
$config['components']['request'] = isset($config['components']['request']) ? array_merge($config['components']['request'], $requestComponent) : $requestComponent;

$responseComponent = [
'class' => \feehi\web\Response::className(),
'swooleResponse' => $response,
];
$config['components']['response'] = isset($config['components']['response']) ? array_merge($config['components']['response'], $responseComponent) : $responseComponent;

$config['components']['assetManager'] = [
$authManagerComponent = [
'class' => yii\web\AssetManager::className(),
'baseUrl' => '/assets'
];
$config['components']['assetManager'] = isset( $config['components']['assetManager'] ) ? array_merge($authManagerComponent, $config['components']['assetManager']) : $authManagerComponent;

$config['components']['session'] = [
"class" => \feehi\web\Session::className()
];

try {
$application = new \yii\web\Application($config);
Expand All @@ -204,8 +210,8 @@ public function actionStart()
}
};

$server->run();
$this->stdout("server is running, listening {$this->host}:{$this->port}" . PHP_EOL);
$server->run();
}

public function actionStop()
Expand Down
27 changes: 25 additions & 2 deletions src/swoole/SwooleServer.php
Expand Up @@ -8,6 +8,7 @@

namespace feehi\swoole;

use feehi\web\Session;

class SwooleServer extends \yii\base\Object
{
Expand All @@ -23,6 +24,7 @@ public function __construct($host, $port, $swooleConfig=[])
self::$swooleConfig = $swooleConfig;
$this->swoole->set($swooleConfig);
$this->swoole->on('request', [$this, 'onRequest']);
$this->swoole->on('WorkerStart', [$this, 'onWorkerStart']);
parent::__construct();
}

Expand All @@ -44,12 +46,19 @@ public function onRequest($request, $response)
//$this->staticRequest($request, $response);

//转换$_FILE超全局变量
$this->mountGlobalFilesVar($request, $response);

$this->mountGlobalFilesVar($request);

call_user_func_array($this->runApp, [$request, $response]);
}

public function onWorkerStart( $serv , $worker_id) {
if( $worker_id == 0 ) {
\swoole_timer_tick(60000, function(){//一分钟清理一次session
(new Session())->gcSession();
});
}
}

/**
* @param \swoole_http_request $request
* @param \swoole_http_response $response
Expand Down Expand Up @@ -116,6 +125,20 @@ private function mountGlobalFilesVar($request)
}
}
}
$_GET = isset($request->get) ? $request->get : [];
$_POST = isset($request->post) ? $request->post : [];
$_COOKIE = isset($request->cookie) ? $request->cookie : [];

$server = isset($request->server) ? $request->server : [];
$header = isset($request->header) ? $request->header : [];
foreach ($server as $key => $value) {
$_SERVER[strtoupper($key)] = $value;
unset($server[$key]);
}
foreach ($header as $key => $value) {
$_SERVER['HTTP_'.strtoupper($key)] = $value;
}
$_SERVER['SERVER_SOFTWARE'] = "swoole/" . SWOOLE_VERSION;
}

}

0 comments on commit 89cf5b2

Please sign in to comment.