提交服务端基础框架
This commit is contained in:
4
Server/vendor/topthink/think-worker/.gitignore
vendored
Normal file
4
Server/vendor/topthink/think-worker/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
/vendor
|
||||
/.idea
|
||||
/.vscode
|
||||
composer.lock
|
||||
201
Server/vendor/topthink/think-worker/LICENSE
vendored
Normal file
201
Server/vendor/topthink/think-worker/LICENSE
vendored
Normal file
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright {yyyy} {name of copyright owner}
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
105
Server/vendor/topthink/think-worker/README.md
vendored
Normal file
105
Server/vendor/topthink/think-worker/README.md
vendored
Normal file
@@ -0,0 +1,105 @@
|
||||
ThinkPHP 5.1 Workerman 扩展
|
||||
===============
|
||||
|
||||
## 安装
|
||||
composer require topthink/think-worker
|
||||
|
||||
## 使用方法
|
||||
|
||||
### HttpServer
|
||||
|
||||
在命令行启动服务端
|
||||
~~~
|
||||
php think worker
|
||||
~~~
|
||||
|
||||
然后就可以通过浏览器直接访问当前应用
|
||||
|
||||
~~~
|
||||
http://localhost:2346
|
||||
~~~
|
||||
|
||||
linux下面可以支持下面指令
|
||||
~~~
|
||||
php think worker [start|stop|reload|restart|status]
|
||||
~~~
|
||||
|
||||
workerman的参数可以在应用配置目录下的worker.php里面配置。
|
||||
|
||||
由于onWorkerStart运行的时候没有HTTP_HOST,因此最好在应用配置文件中设置app_host
|
||||
|
||||
### SocketServer
|
||||
|
||||
在命令行启动服务端
|
||||
~~~
|
||||
php think worker:server
|
||||
~~~
|
||||
|
||||
默认会在0.0.0.0:2345开启一个websocket服务。
|
||||
|
||||
如果需要自定义参数,可以在config/worker_server.php中进行配置,包括:
|
||||
|
||||
配置参数 | 描述
|
||||
--- | ---
|
||||
protocol| 协议
|
||||
host | 监听地址
|
||||
port | 监听端口
|
||||
socket | 完整的socket地址
|
||||
|
||||
并且支持workerman所有的参数。
|
||||
也支持使用闭包方式定义相关事件回调。
|
||||
|
||||
~~~
|
||||
return [
|
||||
'socket' => 'http://127.0.0.1:8000',
|
||||
'name' => 'thinkphp',
|
||||
'count' => 4,
|
||||
'onMessage' => function($connection, $data) {
|
||||
$connection->send(json_encode($data));
|
||||
},
|
||||
];
|
||||
~~~
|
||||
|
||||
也支持使用自定义类作为Worker服务入口文件类。例如,我们可以创建一个服务类(必须要继承 think\worker\Server),然后设置属性和添加回调方法
|
||||
|
||||
~~~
|
||||
<?php
|
||||
namespace app\http;
|
||||
|
||||
use think\worker\Server;
|
||||
|
||||
class Worker extends Server
|
||||
{
|
||||
protected $socket = 'http://0.0.0.0:2346';
|
||||
|
||||
public function onMessage($connection,$data)
|
||||
{
|
||||
$connection->send(json_encode($data));
|
||||
}
|
||||
}
|
||||
~~~
|
||||
支持workerman所有的回调方法定义(回调方法必须是public类型)
|
||||
|
||||
然后在worker_server.php中增加配置参数:
|
||||
~~~
|
||||
return [
|
||||
'worker_class' => 'app\http\Worker',
|
||||
];
|
||||
~~~
|
||||
|
||||
定义该参数后,其它配置参数均不再有效。
|
||||
|
||||
在命令行启动服务端
|
||||
~~~
|
||||
php think worker:server
|
||||
~~~
|
||||
|
||||
然后在浏览器里面访问
|
||||
~~~
|
||||
http://localhost:2346
|
||||
~~~
|
||||
|
||||
如果在Linux下面,同样支持reload|restart|stop|status 操作
|
||||
~~~
|
||||
php think worker:server reload
|
||||
~~~
|
||||
34
Server/vendor/topthink/think-worker/composer.json
vendored
Normal file
34
Server/vendor/topthink/think-worker/composer.json
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "topthink/think-worker",
|
||||
"description": "workerman extend for thinkphp5.1",
|
||||
"license": "Apache-2.0",
|
||||
"type": "think-extend",
|
||||
"authors": [
|
||||
{
|
||||
"name": "liu21st",
|
||||
"email": "liu21st@gmail.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"workerman/workerman": "^3.5.0",
|
||||
"workerman/gateway-worker": "^3.0.0",
|
||||
"topthink/think-installer": "^2.0",
|
||||
"topthink/framework": "^5.1.18",
|
||||
"ext-fileinfo": "*"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"think\\worker\\": "src"
|
||||
},
|
||||
"files": [
|
||||
"src/command.php"
|
||||
]
|
||||
},
|
||||
"extra": {
|
||||
"think-config": {
|
||||
"worker": "src/config/worker.php",
|
||||
"worker_server": "src/config/server.php",
|
||||
"gateway_worker": "src/config/gateway.php"
|
||||
}
|
||||
}
|
||||
}
|
||||
103
Server/vendor/topthink/think-worker/src/Application.php
vendored
Normal file
103
Server/vendor/topthink/think-worker/src/Application.php
vendored
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\worker;
|
||||
|
||||
use think\App;
|
||||
use think\Error;
|
||||
use think\exception\HttpException;
|
||||
use Workerman\Protocols\Http as WorkerHttp;
|
||||
|
||||
/**
|
||||
* Worker应用对象
|
||||
*/
|
||||
class Application extends App
|
||||
{
|
||||
/**
|
||||
* 处理Worker请求
|
||||
* @access public
|
||||
* @param \Workerman\Connection\TcpConnection $connection
|
||||
* @param void
|
||||
*/
|
||||
public function worker($connection)
|
||||
{
|
||||
try {
|
||||
ob_start();
|
||||
// 重置应用的开始时间和内存占用
|
||||
$this->beginTime = microtime(true);
|
||||
$this->beginMem = memory_get_usage();
|
||||
|
||||
// 销毁当前请求对象实例
|
||||
$this->delete('think\Request');
|
||||
|
||||
$pathinfo = ltrim(strpos($_SERVER['REQUEST_URI'], '?') ? strstr($_SERVER['REQUEST_URI'], '?', true) : $_SERVER['REQUEST_URI'], '/');
|
||||
|
||||
$this->request
|
||||
->setPathinfo($pathinfo)
|
||||
->withInput($GLOBALS['HTTP_RAW_REQUEST_DATA']);
|
||||
|
||||
if ($this->config->get('session.auto_start')) {
|
||||
WorkerHttp::sessionStart();
|
||||
}
|
||||
|
||||
// 更新请求对象实例
|
||||
$this->route->setRequest($this->request);
|
||||
|
||||
$response = $this->run();
|
||||
$response->send();
|
||||
$content = ob_get_clean();
|
||||
|
||||
// Trace调试注入
|
||||
if ($this->env->get('app_trace', $this->config->get('app_trace'))) {
|
||||
$this->debug->inject($response, $content);
|
||||
}
|
||||
|
||||
$this->httpResponseCode($response->getCode());
|
||||
|
||||
foreach ($response->getHeader() as $name => $val) {
|
||||
// 发送头部信息
|
||||
WorkerHttp::header($name . (!is_null($val) ? ':' . $val : ''));
|
||||
}
|
||||
|
||||
$connection->send($content);
|
||||
} catch (HttpException $e) {
|
||||
$this->exception($connection, $e);
|
||||
} catch (\Exception $e) {
|
||||
$this->exception($connection, $e);
|
||||
} catch (\Throwable $e) {
|
||||
$this->exception($connection, $e);
|
||||
}
|
||||
}
|
||||
|
||||
protected function httpResponseCode($code = 200)
|
||||
{
|
||||
WorkerHttp::header('HTTP/1.1', true, $code);
|
||||
}
|
||||
|
||||
protected function exception($connection, $e)
|
||||
{
|
||||
if ($e instanceof \Exception) {
|
||||
$handler = Error::getExceptionHandler();
|
||||
$handler->report($e);
|
||||
|
||||
$resp = $handler->render($e);
|
||||
$content = $resp->getContent();
|
||||
$code = $resp->getCode();
|
||||
|
||||
$this->httpResponseCode($code);
|
||||
$connection->send($content);
|
||||
} else {
|
||||
$this->httpResponseCode(500);
|
||||
$connection->send($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
46
Server/vendor/topthink/think-worker/src/Cookie.php
vendored
Normal file
46
Server/vendor/topthink/think-worker/src/Cookie.php
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace think\worker;
|
||||
|
||||
use think\Cookie as BaseCookie;
|
||||
use Workerman\Protocols\Http as WorkerHttp;
|
||||
|
||||
/**
|
||||
* Workerman Cookie类
|
||||
*/
|
||||
class Cookie extends BaseCookie
|
||||
{
|
||||
|
||||
/**
|
||||
* Cookie初始化
|
||||
* @access public
|
||||
* @param array $config
|
||||
* @return void
|
||||
*/
|
||||
public function init(array $config = [])
|
||||
{
|
||||
$this->config = array_merge($this->config, array_change_key_case($config));
|
||||
}
|
||||
|
||||
/**
|
||||
* Cookie 设置保存
|
||||
*
|
||||
* @access public
|
||||
* @param string $name cookie名称
|
||||
* @param mixed $value cookie值
|
||||
* @param array $option 可选参数
|
||||
* @return void
|
||||
*/
|
||||
protected function setCookie($name, $value, $expire, $option = [])
|
||||
{
|
||||
WorkerHttp::setCookie($name, $value, $expire, $option['path'], $option['domain'], $option['secure'], $option['httponly']);
|
||||
}
|
||||
}
|
||||
97
Server/vendor/topthink/think-worker/src/Events.php
vendored
Normal file
97
Server/vendor/topthink/think-worker/src/Events.php
vendored
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace think\worker;
|
||||
|
||||
use GatewayWorker\Lib\Gateway;
|
||||
use Workerman\Worker;
|
||||
|
||||
/**
|
||||
* Worker 命令行服务类
|
||||
*/
|
||||
class Events
|
||||
{
|
||||
/**
|
||||
* onWorkerStart 事件回调
|
||||
* 当businessWorker进程启动时触发。每个进程生命周期内都只会触发一次
|
||||
*
|
||||
* @access public
|
||||
* @param \Workerman\Worker $businessWorker
|
||||
* @return void
|
||||
*/
|
||||
public static function onWorkerStart(Worker $businessWorker)
|
||||
{
|
||||
$app = new Application;
|
||||
$app->initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* onConnect 事件回调
|
||||
* 当客户端连接上gateway进程时(TCP三次握手完毕时)触发
|
||||
*
|
||||
* @access public
|
||||
* @param int $client_id
|
||||
* @return void
|
||||
*/
|
||||
public static function onConnect($client_id)
|
||||
{
|
||||
Gateway::sendToCurrentClient("Your client_id is $client_id");
|
||||
}
|
||||
|
||||
/**
|
||||
* onWebSocketConnect 事件回调
|
||||
* 当客户端连接上gateway完成websocket握手时触发
|
||||
*
|
||||
* @param integer $client_id 断开连接的客户端client_id
|
||||
* @param mixed $data
|
||||
* @return void
|
||||
*/
|
||||
public static function onWebSocketConnect($client_id, $data)
|
||||
{
|
||||
var_export($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* onMessage 事件回调
|
||||
* 当客户端发来数据(Gateway进程收到数据)后触发
|
||||
*
|
||||
* @access public
|
||||
* @param int $client_id
|
||||
* @param mixed $data
|
||||
* @return void
|
||||
*/
|
||||
public static function onMessage($client_id, $data)
|
||||
{
|
||||
Gateway::sendToAll($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* onClose 事件回调 当用户断开连接时触发的方法
|
||||
*
|
||||
* @param integer $client_id 断开连接的客户端client_id
|
||||
* @return void
|
||||
*/
|
||||
public static function onClose($client_id)
|
||||
{
|
||||
GateWay::sendToAll("client[$client_id] logout\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* onWorkerStop 事件回调
|
||||
* 当businessWorker进程退出时触发。每个进程生命周期内都只会触发一次。
|
||||
*
|
||||
* @param \Workerman\Worker $businessWorker
|
||||
* @return void
|
||||
*/
|
||||
public static function onWorkerStop(Worker $businessWorker)
|
||||
{
|
||||
echo "WorkerStop\n";
|
||||
}
|
||||
}
|
||||
284
Server/vendor/topthink/think-worker/src/Http.php
vendored
Normal file
284
Server/vendor/topthink/think-worker/src/Http.php
vendored
Normal file
@@ -0,0 +1,284 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace think\worker;
|
||||
|
||||
use RecursiveDirectoryIterator;
|
||||
use RecursiveIteratorIterator;
|
||||
use think\Facade;
|
||||
use think\Loader;
|
||||
use Workerman\Lib\Timer;
|
||||
use Workerman\Protocols\Http as WorkerHttp;
|
||||
use Workerman\Protocols\Http as HttpProtocols;
|
||||
use Workerman\Worker;
|
||||
|
||||
/**
|
||||
* Worker http server 命令行服务类
|
||||
*/
|
||||
class Http extends Server
|
||||
{
|
||||
protected $app;
|
||||
protected $appPath;
|
||||
protected $root;
|
||||
protected $monitor;
|
||||
protected $lastMtime;
|
||||
/** @var array Mime mapping. */
|
||||
protected static $mimeTypeMap = [];
|
||||
|
||||
/**
|
||||
* 架构函数
|
||||
* @access public
|
||||
* @param string $host 监听地址
|
||||
* @param int $port 监听端口
|
||||
* @param array $context 参数
|
||||
*/
|
||||
public function __construct($host, $port, $context = [])
|
||||
{
|
||||
$this->worker = new Worker('http://' . $host . ':' . $port, $context);
|
||||
|
||||
// 设置回调
|
||||
foreach ($this->event as $event) {
|
||||
if (method_exists($this, $event)) {
|
||||
$this->worker->$event = [$this, $event];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function setRoot($root)
|
||||
{
|
||||
$this->root = $root;
|
||||
}
|
||||
|
||||
public function setAppPath($path)
|
||||
{
|
||||
$this->appPath = $path;
|
||||
}
|
||||
|
||||
public function setStaticOption($name, $value)
|
||||
{
|
||||
Worker::${$name} = $value;
|
||||
}
|
||||
|
||||
public function setMonitor($interval = 2, $path = [])
|
||||
{
|
||||
$this->monitor['interval'] = $interval;
|
||||
$this->monitor['path'] = (array) $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置参数
|
||||
* @access public
|
||||
* @param array $option 参数
|
||||
* @return void
|
||||
*/
|
||||
public function option(array $option)
|
||||
{
|
||||
// 设置参数
|
||||
if (!empty($option)) {
|
||||
foreach ($option as $key => $val) {
|
||||
$this->worker->$key = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* onWorkerStart 事件回调
|
||||
* @access public
|
||||
* @param \Workerman\Worker $worker
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function onWorkerStart($worker)
|
||||
{
|
||||
$this->initMimeTypeMap();
|
||||
$this->app = new Application($this->appPath);
|
||||
$this->lastMtime = time();
|
||||
|
||||
$this->app->workerman = $worker;
|
||||
|
||||
// 指定日志类驱动
|
||||
Loader::addClassMap([
|
||||
'think\\log\\driver\\File' => __DIR__ . '/log/File.php',
|
||||
]);
|
||||
|
||||
Facade::bind([
|
||||
'think\facade\Cookie' => Cookie::class,
|
||||
'think\facade\Session' => Session::class,
|
||||
facade\Application::class => Application::class,
|
||||
facade\Http::class => Http::class,
|
||||
]);
|
||||
|
||||
// 应用初始化
|
||||
$this->app->initialize();
|
||||
|
||||
$this->app->bindTo([
|
||||
'cookie' => Cookie::class,
|
||||
'session' => Session::class,
|
||||
]);
|
||||
|
||||
if (0 == $worker->id && $this->monitor) {
|
||||
$paths = $this->monitor['path'] ?: [$this->app->getAppPath(), $this->app->getConfigPath()];
|
||||
$timer = $this->monitor['interval'] ?: 2;
|
||||
|
||||
Timer::add($timer, function () use ($paths) {
|
||||
foreach ($paths as $path) {
|
||||
$dir = new RecursiveDirectoryIterator($path);
|
||||
$iterator = new RecursiveIteratorIterator($dir);
|
||||
|
||||
foreach ($iterator as $file) {
|
||||
if (pathinfo($file, PATHINFO_EXTENSION) != 'php') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($this->lastMtime < $file->getMTime()) {
|
||||
echo '[update]' . $file . "\n";
|
||||
posix_kill(posix_getppid(), SIGUSR1);
|
||||
$this->lastMtime = $file->getMTime();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* onMessage 事件回调
|
||||
* @access public
|
||||
* @param \Workerman\Connection\TcpConnection $connection
|
||||
* @param mixed $data
|
||||
* @return void
|
||||
*/
|
||||
public function onMessage($connection, $data)
|
||||
{
|
||||
$uri = parse_url($_SERVER['REQUEST_URI']);
|
||||
$path = isset($uri['path']) ? $uri['path'] : '/';
|
||||
|
||||
$file = $this->root . $path;
|
||||
|
||||
if (!is_file($file)) {
|
||||
$this->app->worker($connection, $data);
|
||||
} else {
|
||||
$this->sendFile($connection, $file);
|
||||
}
|
||||
}
|
||||
|
||||
protected function sendFile($connection, $file)
|
||||
{
|
||||
$info = stat($file);
|
||||
$modifiyTime = $info ? date('D, d M Y H:i:s', $info['mtime']) . ' ' . date_default_timezone_get() : '';
|
||||
|
||||
if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $info) {
|
||||
// Http 304.
|
||||
if ($modifiyTime === $_SERVER['HTTP_IF_MODIFIED_SINCE']) {
|
||||
// 304
|
||||
WorkerHttp::header('HTTP/1.1 304 Not Modified');
|
||||
// Send nothing but http headers..
|
||||
return $connection->close('');
|
||||
}
|
||||
}
|
||||
|
||||
$mimeType = $this->getMimeType($file);
|
||||
|
||||
WorkerHttp::header('HTTP/1.1 200 OK');
|
||||
WorkerHttp::header('Connection: keep-alive');
|
||||
|
||||
if ($mimeType) {
|
||||
WorkerHttp::header('Content-Type: ' . $mimeType);
|
||||
} else {
|
||||
WorkerHttp::header('Content-Type: application/octet-stream');
|
||||
$fileinfo = pathinfo($file);
|
||||
$filename = isset($fileinfo['filename']) ? $fileinfo['filename'] : '';
|
||||
WorkerHttp::header('Content-Disposition: attachment; filename="' . $filename . '"');
|
||||
}
|
||||
|
||||
if ($modifiyTime) {
|
||||
WorkerHttp::header('Last-Modified: ' . $modifiyTime);
|
||||
}
|
||||
|
||||
WorkerHttp::header('Content-Length: ' . filesize($file));
|
||||
|
||||
ob_start();
|
||||
readfile($file);
|
||||
$content = ob_get_clean();
|
||||
|
||||
return $connection->send($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件类型信息
|
||||
* @access public
|
||||
* @param $filename
|
||||
* @return string
|
||||
*/
|
||||
public function getMimeType($filename)
|
||||
{
|
||||
$file_info = pathinfo($filename);
|
||||
$extension = isset($file_info['extension']) ? $file_info['extension'] : '';
|
||||
|
||||
if (isset(self::$mimeTypeMap[$extension])) {
|
||||
$mime = self::$mimeTypeMap[$extension];
|
||||
} else {
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
$mime = finfo_file($finfo, $filename);
|
||||
}
|
||||
return $mime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
Worker::runAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function stop()
|
||||
{
|
||||
Worker::stopAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Init mime map.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function initMimeTypeMap()
|
||||
{
|
||||
$mime_file = HttpProtocols::getMimeTypesFile();
|
||||
if (!is_file($mime_file)) {
|
||||
Worker::log("$mime_file mime.type file not fond");
|
||||
return;
|
||||
}
|
||||
$items = file($mime_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
if (!is_array($items)) {
|
||||
Worker::log("get $mime_file mime.type content fail");
|
||||
return;
|
||||
}
|
||||
foreach ($items as $content) {
|
||||
if (preg_match("/\s*(\S+)\s+(\S.+)/", $content, $match)) {
|
||||
$mime_type = $match[1];
|
||||
$workerman_file_extension_var = $match[2];
|
||||
$workerman_file_extension_array = explode(' ', substr($workerman_file_extension_var, 0, -1));
|
||||
foreach ($workerman_file_extension_array as $workerman_file_extension) {
|
||||
self::$mimeTypeMap[$workerman_file_extension] = $mime_type;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
75
Server/vendor/topthink/think-worker/src/Server.php
vendored
Normal file
75
Server/vendor/topthink/think-worker/src/Server.php
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\worker;
|
||||
|
||||
use Workerman\Worker;
|
||||
|
||||
/**
|
||||
* Worker控制器扩展类
|
||||
*/
|
||||
abstract class Server
|
||||
{
|
||||
protected $worker;
|
||||
protected $socket = '';
|
||||
protected $protocol = 'http';
|
||||
protected $host = '0.0.0.0';
|
||||
protected $port = '2346';
|
||||
protected $option = [];
|
||||
protected $context = [];
|
||||
protected $event = ['onWorkerStart', 'onConnect', 'onMessage', 'onClose', 'onError', 'onBufferFull', 'onBufferDrain', 'onWorkerReload', 'onWebSocketConnect'];
|
||||
|
||||
/**
|
||||
* 架构函数
|
||||
* @access public
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
// 实例化 Websocket 服务
|
||||
$this->worker = new Worker($this->socket ?: $this->protocol . '://' . $this->host . ':' . $this->port, $this->context);
|
||||
|
||||
// 设置参数
|
||||
if (!empty($this->option)) {
|
||||
foreach ($this->option as $key => $val) {
|
||||
$this->worker->$key = $val;
|
||||
}
|
||||
}
|
||||
|
||||
// 设置回调
|
||||
foreach ($this->event as $event) {
|
||||
if (method_exists($this, $event)) {
|
||||
$this->worker->$event = [$this, $event];
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化
|
||||
$this->init();
|
||||
}
|
||||
|
||||
protected function init()
|
||||
{
|
||||
}
|
||||
|
||||
public function start()
|
||||
{
|
||||
Worker::runAll();
|
||||
}
|
||||
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$this->worker->$name = $value;
|
||||
}
|
||||
|
||||
public function __call($method, $args)
|
||||
{
|
||||
call_user_func_array([$this->worker, $method], $args);
|
||||
}
|
||||
}
|
||||
157
Server/vendor/topthink/think-worker/src/Session.php
vendored
Normal file
157
Server/vendor/topthink/think-worker/src/Session.php
vendored
Normal file
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace think\worker;
|
||||
|
||||
use think\Session as BaseSession;
|
||||
use Workerman\Protocols\Http as WorkerHttp;
|
||||
|
||||
/**
|
||||
* Workerman Cookie类
|
||||
*/
|
||||
class Session extends BaseSession
|
||||
{
|
||||
/**
|
||||
* session初始化
|
||||
* @access public
|
||||
* @param array $config
|
||||
* @return void
|
||||
* @throws \think\Exception
|
||||
*/
|
||||
public function init(array $config = [])
|
||||
{
|
||||
$config = $config ?: $this->config;
|
||||
|
||||
$isDoStart = false;
|
||||
if (isset($config['use_trans_sid'])) {
|
||||
ini_set('session.use_trans_sid', $config['use_trans_sid'] ? 1 : 0);
|
||||
}
|
||||
|
||||
// 启动session
|
||||
if (!empty($config['auto_start']) && PHP_SESSION_ACTIVE != session_status()) {
|
||||
ini_set('session.auto_start', 0);
|
||||
$isDoStart = true;
|
||||
}
|
||||
|
||||
if (isset($config['prefix'])) {
|
||||
$this->prefix = $config['prefix'];
|
||||
}
|
||||
|
||||
if (isset($config['use_lock'])) {
|
||||
$this->lock = $config['use_lock'];
|
||||
}
|
||||
|
||||
if (isset($config['var_session_id']) && isset($_REQUEST[$config['var_session_id']])) {
|
||||
session_id($_REQUEST[$config['var_session_id']]);
|
||||
} elseif (isset($config['id']) && !empty($config['id'])) {
|
||||
session_id($config['id']);
|
||||
} else {
|
||||
session_id(WorkerHttp::sessionId());
|
||||
}
|
||||
|
||||
if (isset($config['name'])) {
|
||||
WorkerHttp::sessionName($config['name']);
|
||||
}
|
||||
|
||||
if (isset($config['path'])) {
|
||||
WorkerHttp::sessionSavePath($config['path']);
|
||||
}
|
||||
|
||||
if (isset($config['domain'])) {
|
||||
ini_set('session.cookie_domain', $config['domain']);
|
||||
}
|
||||
|
||||
if (isset($config['expire'])) {
|
||||
ini_set('session.gc_maxlifetime', $config['expire']);
|
||||
ini_set('session.cookie_lifetime', $config['expire']);
|
||||
}
|
||||
|
||||
if (isset($config['secure'])) {
|
||||
ini_set('session.cookie_secure', $config['secure']);
|
||||
}
|
||||
|
||||
if (isset($config['httponly'])) {
|
||||
ini_set('session.cookie_httponly', $config['httponly']);
|
||||
}
|
||||
|
||||
if (isset($config['use_cookies'])) {
|
||||
ini_set('session.use_cookies', $config['use_cookies'] ? 1 : 0);
|
||||
}
|
||||
|
||||
if (isset($config['cache_limiter'])) {
|
||||
session_cache_limiter($config['cache_limiter']);
|
||||
}
|
||||
|
||||
if (isset($config['cache_expire'])) {
|
||||
session_cache_expire($config['cache_expire']);
|
||||
}
|
||||
|
||||
if (!empty($config['type'])) {
|
||||
// 读取session驱动
|
||||
$class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\session\\driver\\' . ucwords($config['type']);
|
||||
|
||||
// 检查驱动类
|
||||
if (!class_exists($class) || !session_set_save_handler(new $class($config))) {
|
||||
throw new ClassNotFoundException('error session handler:' . $class, $class);
|
||||
}
|
||||
}
|
||||
|
||||
if ($isDoStart) {
|
||||
$this->start();
|
||||
} else {
|
||||
$this->init = false;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* session自动启动或者初始化
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
if (is_null($this->init)) {
|
||||
$this->init();
|
||||
}
|
||||
|
||||
if (false === $this->init) {
|
||||
if (PHP_SESSION_ACTIVE != session_status()) {
|
||||
$this->start();
|
||||
}
|
||||
$this->init = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动session
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
WorkerHttp::sessionStart();
|
||||
|
||||
$this->init = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 暂停session
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function pause()
|
||||
{
|
||||
// 暂停session
|
||||
WorkerHttp::sessionWriteClose();
|
||||
$this->init = false;
|
||||
}
|
||||
}
|
||||
16
Server/vendor/topthink/think-worker/src/command.php
vendored
Normal file
16
Server/vendor/topthink/think-worker/src/command.php
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
\think\Console::addDefaultCommands([
|
||||
'worker:gateway' => '\\think\\worker\\command\\GatewayWorker',
|
||||
'worker:server' => '\\think\\worker\\command\\Server',
|
||||
'worker' => '\\think\\worker\\command\\Worker',
|
||||
]);
|
||||
201
Server/vendor/topthink/think-worker/src/command/GatewayWorker.php
vendored
Normal file
201
Server/vendor/topthink/think-worker/src/command/GatewayWorker.php
vendored
Normal file
@@ -0,0 +1,201 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\worker\command;
|
||||
|
||||
use GatewayWorker\BusinessWorker;
|
||||
use GatewayWorker\Gateway;
|
||||
use GatewayWorker\Register;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Argument;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
use think\facade\Config;
|
||||
use Workerman\Worker;
|
||||
|
||||
/**
|
||||
* Worker 命令行类
|
||||
*/
|
||||
class GatewayWorker extends Command
|
||||
{
|
||||
public function configure()
|
||||
{
|
||||
$this->setName('worker:gateway')
|
||||
->addArgument('action', Argument::OPTIONAL, "start|stop|restart|reload|status|connections", 'start')
|
||||
->addOption('host', 'H', Option::VALUE_OPTIONAL, 'the host of workerman server.', null)
|
||||
->addOption('port', 'p', Option::VALUE_OPTIONAL, 'the port of workerman server.', null)
|
||||
->addOption('daemon', 'd', Option::VALUE_NONE, 'Run the workerman server in daemon mode.')
|
||||
->setDescription('GatewayWorker Server for ThinkPHP');
|
||||
}
|
||||
|
||||
public function execute(Input $input, Output $output)
|
||||
{
|
||||
$action = $input->getArgument('action');
|
||||
|
||||
if (DIRECTORY_SEPARATOR !== '\\') {
|
||||
if (!in_array($action, ['start', 'stop', 'reload', 'restart', 'status', 'connections'])) {
|
||||
$output->writeln("Invalid argument action:{$action}, Expected start|stop|restart|reload|status|connections .");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
global $argv;
|
||||
array_shift($argv);
|
||||
array_shift($argv);
|
||||
array_unshift($argv, 'think', $action);
|
||||
} else {
|
||||
$output->writeln("GatewayWorker Not Support On Windows.");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if ('start' == $action) {
|
||||
$output->writeln('Starting GatewayWorker server...');
|
||||
}
|
||||
|
||||
$option = Config::pull('gateway_worker');
|
||||
|
||||
if ($input->hasOption('host')) {
|
||||
$host = $input->getOption('host');
|
||||
} else {
|
||||
$host = !empty($option['host']) ? $option['host'] : '0.0.0.0';
|
||||
}
|
||||
|
||||
if ($input->hasOption('port')) {
|
||||
$port = $input->getOption('port');
|
||||
} else {
|
||||
$port = !empty($option['port']) ? $option['port'] : '2347';
|
||||
}
|
||||
|
||||
$this->start($host, $port, $option);
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动
|
||||
* @access public
|
||||
* @param string $host 监听地址
|
||||
* @param integer $port 监听端口
|
||||
* @param array $option 参数
|
||||
* @return void
|
||||
*/
|
||||
public function start($host, $port, $option = [])
|
||||
{
|
||||
$registerAddress = !empty($option['registerAddress']) ? $option['registerAddress'] : '127.0.0.1:1236';
|
||||
|
||||
if (!empty($option['register_deploy'])) {
|
||||
// 分布式部署的时候其它服务器可以关闭register服务
|
||||
// 注意需要设置不同的lanIp
|
||||
$this->register($registerAddress);
|
||||
}
|
||||
|
||||
// 启动businessWorker
|
||||
if (!empty($option['businessWorker_deploy'])) {
|
||||
$this->businessWorker($registerAddress, isset($option['businessWorker']) ? $option['businessWorker'] : []);
|
||||
}
|
||||
|
||||
// 启动gateway
|
||||
if (!empty($option['gateway_deploy'])) {
|
||||
$this->gateway($registerAddress, $host, $port, $option);
|
||||
}
|
||||
|
||||
Worker::runAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动register
|
||||
* @access public
|
||||
* @param string $registerAddress
|
||||
* @return void
|
||||
*/
|
||||
public function register($registerAddress)
|
||||
{
|
||||
// 初始化register
|
||||
new Register('text://' . $registerAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动businessWorker
|
||||
* @access public
|
||||
* @param string $registerAddress registerAddress
|
||||
* @param array $option 参数
|
||||
* @return void
|
||||
*/
|
||||
public function businessWorker($registerAddress, $option = [])
|
||||
{
|
||||
// 初始化 bussinessWorker 进程
|
||||
$worker = new BusinessWorker();
|
||||
|
||||
$this->option($worker, $option);
|
||||
|
||||
$worker->registerAddress = $registerAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动gateway
|
||||
* @access public
|
||||
* @param string $registerAddress registerAddress
|
||||
* @param string $host 服务地址
|
||||
* @param integer $port 监听端口
|
||||
* @param array $option 参数
|
||||
* @return void
|
||||
*/
|
||||
public function gateway($registerAddress, $host, $port, $option = [])
|
||||
{
|
||||
// 初始化 gateway 进程
|
||||
if (!empty($option['socket'])) {
|
||||
$socket = $option['socket'];
|
||||
unset($option['socket']);
|
||||
} else {
|
||||
$protocol = !empty($option['protocol']) ? $option['protocol'] : 'websocket';
|
||||
$socket = $protocol . '://' . $host . ':' . $port;
|
||||
unset($option['host'], $option['port'], $option['protocol']);
|
||||
}
|
||||
|
||||
$gateway = new Gateway($socket, isset($option['context']) ? $option['context'] : []);
|
||||
|
||||
// 以下设置参数都可以在配置文件中重新定义覆盖
|
||||
$gateway->name = 'Gateway';
|
||||
$gateway->count = 4;
|
||||
$gateway->lanIp = '127.0.0.1';
|
||||
$gateway->startPort = 2000;
|
||||
$gateway->pingInterval = 30;
|
||||
$gateway->pingNotResponseLimit = 0;
|
||||
$gateway->pingData = '{"type":"ping"}';
|
||||
$gateway->registerAddress = $registerAddress;
|
||||
|
||||
// 全局静态属性设置
|
||||
foreach ($option as $name => $val) {
|
||||
if (in_array($name, ['stdoutFile', 'daemonize', 'pidFile', 'logFile'])) {
|
||||
Worker::${$name} = $val;
|
||||
unset($option[$name]);
|
||||
}
|
||||
}
|
||||
|
||||
$this->option($gateway, $option);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置参数
|
||||
* @access protected
|
||||
* @param Worker $worker Worker对象
|
||||
* @param array $option 参数
|
||||
* @return void
|
||||
*/
|
||||
protected function option($worker, array $option = [])
|
||||
{
|
||||
// 设置参数
|
||||
if (!empty($option)) {
|
||||
foreach ($option as $key => $val) {
|
||||
$worker->$key = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
162
Server/vendor/topthink/think-worker/src/command/Server.php
vendored
Normal file
162
Server/vendor/topthink/think-worker/src/command/Server.php
vendored
Normal file
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\worker\command;
|
||||
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Argument;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
use think\facade\Config;
|
||||
use think\facade\Env;
|
||||
use think\worker\Server as WorkerServer;
|
||||
use Workerman\Worker;
|
||||
|
||||
/**
|
||||
* Worker Server 命令行类
|
||||
*/
|
||||
class Server extends Command
|
||||
{
|
||||
protected $config = [];
|
||||
|
||||
public function configure()
|
||||
{
|
||||
$this->setName('worker:server')
|
||||
->addArgument('action', Argument::OPTIONAL, "start|stop|restart|reload|status|connections", 'start')
|
||||
->addOption('host', 'H', Option::VALUE_OPTIONAL, 'the host of workerman server.', null)
|
||||
->addOption('port', 'p', Option::VALUE_OPTIONAL, 'the port of workerman server.', null)
|
||||
->addOption('daemon', 'd', Option::VALUE_NONE, 'Run the workerman server in daemon mode.')
|
||||
->setDescription('Workerman Server for ThinkPHP');
|
||||
}
|
||||
|
||||
public function execute(Input $input, Output $output)
|
||||
{
|
||||
$action = $input->getArgument('action');
|
||||
|
||||
if (DIRECTORY_SEPARATOR !== '\\') {
|
||||
if (!in_array($action, ['start', 'stop', 'reload', 'restart', 'status', 'connections'])) {
|
||||
$output->writeln("<error>Invalid argument action:{$action}, Expected start|stop|restart|reload|status|connections .</error>");
|
||||
return false;
|
||||
}
|
||||
|
||||
global $argv;
|
||||
array_shift($argv);
|
||||
array_shift($argv);
|
||||
array_unshift($argv, 'think', $action);
|
||||
} elseif ('start' != $action) {
|
||||
$output->writeln("<error>Not Support action:{$action} on Windows.</error>");
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->config = Config::pull('worker_server');
|
||||
|
||||
if ('start' == $action) {
|
||||
$output->writeln('Starting Workerman server...');
|
||||
}
|
||||
|
||||
// 自定义服务器入口类
|
||||
if (!empty($this->config['worker_class'])) {
|
||||
$class = (array) $this->config['worker_class'];
|
||||
|
||||
foreach ($class as $server) {
|
||||
$this->startServer($server);
|
||||
}
|
||||
|
||||
// Run worker
|
||||
Worker::runAll();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!empty($this->config['socket'])) {
|
||||
$socket = $this->config['socket'];
|
||||
list($host, $port) = explode(':', $socket);
|
||||
} else {
|
||||
$host = $this->getHost();
|
||||
$port = $this->getPort();
|
||||
$protocol = !empty($this->config['protocol']) ? $this->config['protocol'] : 'websocket';
|
||||
$socket = $protocol . '://' . $host . ':' . $port;
|
||||
unset($this->config['host'], $this->config['port'], $this->config['protocol']);
|
||||
}
|
||||
|
||||
if (isset($this->config['context'])) {
|
||||
$context = $this->config['context'];
|
||||
unset($this->config['context']);
|
||||
} else {
|
||||
$context = [];
|
||||
}
|
||||
|
||||
$worker = new Worker($socket, $context);
|
||||
|
||||
if (empty($this->config['pidFile'])) {
|
||||
$this->config['pidFile'] = Env::get('runtime_path') . 'worker.pid';
|
||||
}
|
||||
|
||||
// 避免pid混乱
|
||||
$this->config['pidFile'] .= '_' . $port;
|
||||
|
||||
// 开启守护进程模式
|
||||
if ($this->input->hasOption('daemon')) {
|
||||
Worker::$daemonize = true;
|
||||
}
|
||||
|
||||
if (!empty($this->config['ssl'])) {
|
||||
$this->config['transport'] = 'ssl';
|
||||
unset($this->config['ssl']);
|
||||
}
|
||||
|
||||
// 设置服务器参数
|
||||
foreach ($this->config as $name => $val) {
|
||||
if (in_array($name, ['stdoutFile', 'daemonize', 'pidFile', 'logFile'])) {
|
||||
Worker::${$name} = $val;
|
||||
} else {
|
||||
$worker->$name = $val;
|
||||
}
|
||||
}
|
||||
|
||||
// Run worker
|
||||
Worker::runAll();
|
||||
}
|
||||
|
||||
protected function startServer($class)
|
||||
{
|
||||
if (class_exists($class)) {
|
||||
$worker = new $class;
|
||||
if (!$worker instanceof WorkerServer) {
|
||||
$this->output->writeln("<error>Worker Server Class Must extends \\think\\worker\\Server</error>");
|
||||
}
|
||||
} else {
|
||||
$this->output->writeln("<error>Worker Server Class Not Exists : {$class}</error>");
|
||||
}
|
||||
}
|
||||
|
||||
protected function getHost()
|
||||
{
|
||||
if ($this->input->hasOption('host')) {
|
||||
$host = $this->input->getOption('host');
|
||||
} else {
|
||||
$host = !empty($this->config['host']) ? $this->config['host'] : '0.0.0.0';
|
||||
}
|
||||
|
||||
return $host;
|
||||
}
|
||||
|
||||
protected function getPort()
|
||||
{
|
||||
if ($this->input->hasOption('port')) {
|
||||
$port = $this->input->getOption('port');
|
||||
} else {
|
||||
$port = !empty($this->config['port']) ? $this->config['port'] : 2345;
|
||||
}
|
||||
|
||||
return $port;
|
||||
}
|
||||
}
|
||||
154
Server/vendor/topthink/think-worker/src/command/Worker.php
vendored
Normal file
154
Server/vendor/topthink/think-worker/src/command/Worker.php
vendored
Normal file
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\worker\command;
|
||||
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Argument;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
use think\facade\Config;
|
||||
use think\facade\Env;
|
||||
use think\worker\Http as HttpServer;
|
||||
|
||||
/**
|
||||
* Worker 命令行类
|
||||
*/
|
||||
class Worker extends Command
|
||||
{
|
||||
protected $config = [];
|
||||
|
||||
public function configure()
|
||||
{
|
||||
$this->setName('worker')
|
||||
->addArgument('action', Argument::OPTIONAL, "start|stop|restart|reload|status|connections", 'start')
|
||||
->addOption('host', 'H', Option::VALUE_OPTIONAL, 'the host of workerman server.', null)
|
||||
->addOption('port', 'p', Option::VALUE_OPTIONAL, 'the port of workerman server.', null)
|
||||
->addOption('daemon', 'd', Option::VALUE_NONE, 'Run the workerman server in daemon mode.')
|
||||
->setDescription('Workerman HTTP Server for ThinkPHP');
|
||||
}
|
||||
|
||||
public function execute(Input $input, Output $output)
|
||||
{
|
||||
$action = $input->getArgument('action');
|
||||
|
||||
if (DIRECTORY_SEPARATOR !== '\\') {
|
||||
if (!in_array($action, ['start', 'stop', 'reload', 'restart', 'status', 'connections'])) {
|
||||
$output->writeln("<error>Invalid argument action:{$action}, Expected start|stop|restart|reload|status|connections .</error>");
|
||||
return false;
|
||||
}
|
||||
|
||||
global $argv;
|
||||
array_shift($argv);
|
||||
array_shift($argv);
|
||||
array_unshift($argv, 'think', $action);
|
||||
} elseif ('start' != $action) {
|
||||
$output->writeln("<error>Not Support action:{$action} on Windows.</error>");
|
||||
return false;
|
||||
}
|
||||
|
||||
if ('start' == $action) {
|
||||
$output->writeln('Starting Workerman http server...');
|
||||
}
|
||||
|
||||
$this->config = Config::pull('worker');
|
||||
|
||||
if (isset($this->config['context'])) {
|
||||
$context = $this->config['context'];
|
||||
unset($this->config['context']);
|
||||
} else {
|
||||
$context = [];
|
||||
}
|
||||
|
||||
$host = $this->getHost();
|
||||
$port = $this->getPort();
|
||||
|
||||
$worker = new HttpServer($host, $port, $context);
|
||||
|
||||
if (empty($this->config['pidFile'])) {
|
||||
$this->config['pidFile'] = Env::get('runtime_path') . 'worker.pid';
|
||||
}
|
||||
|
||||
// 避免pid混乱
|
||||
$this->config['pidFile'] .= '_' . $port;
|
||||
|
||||
// 设置应用目录
|
||||
$worker->setAppPath($this->config['app_path']);
|
||||
unset($this->config['app_path']);
|
||||
|
||||
// 开启守护进程模式
|
||||
if ($this->input->hasOption('daemon')) {
|
||||
$worker->setStaticOption('daemonize', true);
|
||||
}
|
||||
|
||||
// 开启HTTPS访问
|
||||
if (!empty($this->config['ssl'])) {
|
||||
$this->config['transport'] = 'ssl';
|
||||
unset($this->config['ssl']);
|
||||
}
|
||||
|
||||
// 设置网站目录
|
||||
if (empty($this->config['root'])) {
|
||||
$this->config['root'] = Env::get('root_path') . 'public';
|
||||
}
|
||||
|
||||
$worker->setRoot($this->config['root']);
|
||||
unset($this->config['root']);
|
||||
|
||||
// 设置文件监控
|
||||
if (DIRECTORY_SEPARATOR !== '\\' && (Env::get('app_debug') || !empty($this->config['file_monitor']))) {
|
||||
$interval = isset($this->config['file_monitor_interval']) ? $this->config['file_monitor_interval'] : 2;
|
||||
$paths = isset($this->config['file_monitor_path']) ? $this->config['file_monitor_path'] : [];
|
||||
$worker->setMonitor($interval, $paths);
|
||||
unset($this->config['file_monitor'], $this->config['file_monitor_interval'], $this->config['file_monitor_path']);
|
||||
}
|
||||
|
||||
// 全局静态属性设置
|
||||
foreach ($this->config as $name => $val) {
|
||||
if (in_array($name, ['stdoutFile', 'daemonize', 'pidFile', 'logFile'])) {
|
||||
$worker->setStaticOption($name, $val);
|
||||
unset($this->config[$name]);
|
||||
}
|
||||
}
|
||||
|
||||
// 设置服务器参数
|
||||
$worker->option($this->config);
|
||||
|
||||
if (DIRECTORY_SEPARATOR == '\\') {
|
||||
$output->writeln('You can exit with <info>`CTRL-C`</info>');
|
||||
}
|
||||
|
||||
$worker->start();
|
||||
}
|
||||
|
||||
protected function getHost($default = '0.0.0.0')
|
||||
{
|
||||
if ($this->input->hasOption('host')) {
|
||||
$host = $this->input->getOption('host');
|
||||
} else {
|
||||
$host = !empty($this->config['host']) ? $this->config['host'] : $default;
|
||||
}
|
||||
|
||||
return $host;
|
||||
}
|
||||
|
||||
protected function getPort($default = '2346')
|
||||
{
|
||||
if ($this->input->hasOption('port')) {
|
||||
$port = $this->input->getOption('port');
|
||||
} else {
|
||||
$port = !empty($this->config['port']) ? $this->config['port'] : $default;
|
||||
}
|
||||
|
||||
return $port;
|
||||
}
|
||||
}
|
||||
45
Server/vendor/topthink/think-worker/src/config/gateway.php
vendored
Normal file
45
Server/vendor/topthink/think-worker/src/config/gateway.php
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
// +----------------------------------------------------------------------
|
||||
// | Workerman设置 仅对 php think worker:gateway 指令有效
|
||||
// +----------------------------------------------------------------------
|
||||
return [
|
||||
// 扩展自身需要的配置
|
||||
'protocol' => 'websocket', // 协议 支持 tcp udp unix http websocket text
|
||||
'host' => '0.0.0.0', // 监听地址
|
||||
'port' => 2348, // 监听端口
|
||||
'socket' => '', // 完整监听地址
|
||||
'context' => [], // socket 上下文选项
|
||||
'register_deploy' => true, // 是否需要部署register
|
||||
'businessWorker_deploy' => true, // 是否需要部署businessWorker
|
||||
'gateway_deploy' => true, // 是否需要部署gateway
|
||||
|
||||
// Register配置
|
||||
'registerAddress' => '127.0.0.1:1236',
|
||||
|
||||
// Gateway配置
|
||||
'name' => 'thinkphp',
|
||||
'count' => 1,
|
||||
'lanIp' => '127.0.0.1',
|
||||
'startPort' => 2000,
|
||||
'daemonize' => false,
|
||||
'pingInterval' => 30,
|
||||
'pingNotResponseLimit' => 0,
|
||||
'pingData' => '{"type":"ping"}',
|
||||
|
||||
// BusinsessWorker配置
|
||||
'businessWorker' => [
|
||||
'name' => 'BusinessWorker',
|
||||
'count' => 1,
|
||||
'eventHandler' => '\think\worker\Events',
|
||||
],
|
||||
|
||||
];
|
||||
56
Server/vendor/topthink/think-worker/src/config/server.php
vendored
Normal file
56
Server/vendor/topthink/think-worker/src/config/server.php
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
use think\facade\Env;
|
||||
|
||||
// +----------------------------------------------------------------------
|
||||
// | Workerman设置 仅对 php think worker:server 指令有效
|
||||
// +----------------------------------------------------------------------
|
||||
return [
|
||||
// 扩展自身需要的配置
|
||||
'protocol' => 'websocket', // 协议 支持 tcp udp unix http websocket text
|
||||
'host' => '0.0.0.0', // 监听地址
|
||||
'port' => 2345, // 监听端口
|
||||
'socket' => '', // 完整监听地址
|
||||
'context' => [], // socket 上下文选项
|
||||
'worker_class' => '', // 自定义Workerman服务类名 支持数组定义多个服务
|
||||
|
||||
// 支持workerman的所有配置参数
|
||||
'name' => 'thinkphp',
|
||||
'count' => 4,
|
||||
'daemonize' => false,
|
||||
'pidFile' => Env::get('runtime_path') . 'worker.pid',
|
||||
|
||||
// 支持事件回调
|
||||
// onWorkerStart
|
||||
'onWorkerStart' => function ($worker) {
|
||||
|
||||
},
|
||||
// onWorkerReload
|
||||
'onWorkerReload' => function ($worker) {
|
||||
|
||||
},
|
||||
// onConnect
|
||||
'onConnect' => function ($connection) {
|
||||
|
||||
},
|
||||
// onMessage
|
||||
'onMessage' => function ($connection, $data) {
|
||||
$connection->send('receive success');
|
||||
},
|
||||
// onClose
|
||||
'onClose' => function ($connection) {
|
||||
|
||||
},
|
||||
// onError
|
||||
'onError' => function ($connection, $code, $msg) {
|
||||
echo "error [ $code ] $msg\n";
|
||||
},
|
||||
];
|
||||
31
Server/vendor/topthink/think-worker/src/config/worker.php
vendored
Normal file
31
Server/vendor/topthink/think-worker/src/config/worker.php
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
use think\facade\Env;
|
||||
|
||||
// +----------------------------------------------------------------------
|
||||
// | Workerman设置 仅对 php think worker 指令有效
|
||||
// +----------------------------------------------------------------------
|
||||
return [
|
||||
// 扩展自身需要的配置
|
||||
'host' => '0.0.0.0', // 监听地址
|
||||
'port' => 2346, // 监听端口
|
||||
'root' => '', // WEB 根目录 默认会定位public目录
|
||||
'app_path' => '', // 应用目录 守护进程模式必须设置(绝对路径)
|
||||
'file_monitor' => false, // 是否开启PHP文件更改监控(调试模式下自动开启)
|
||||
'file_monitor_interval' => 2, // 文件监控检测时间间隔(秒)
|
||||
'file_monitor_path' => [], // 文件监控目录 默认监控application和config目录
|
||||
|
||||
// 支持workerman的所有配置参数
|
||||
'name' => 'thinkphp',
|
||||
'count' => 4,
|
||||
'daemonize' => false,
|
||||
'pidFile' => Env::get('runtime_path') . 'worker.pid',
|
||||
];
|
||||
24
Server/vendor/topthink/think-worker/src/facade/Application.php
vendored
Normal file
24
Server/vendor/topthink/think-worker/src/facade/Application.php
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\worker\facade;
|
||||
|
||||
use think\Facade;
|
||||
|
||||
/**
|
||||
* @see \think\worker\Application
|
||||
* @mixin \think\worker\Application
|
||||
* @method void initialize() static 初始化应用
|
||||
* @method void worker(\Workerman\Connection\TcpConnection $connection) static 处理Worker请求
|
||||
*/
|
||||
class Application extends Facade
|
||||
{
|
||||
}
|
||||
25
Server/vendor/topthink/think-worker/src/facade/Worker.php
vendored
Normal file
25
Server/vendor/topthink/think-worker/src/facade/Worker.php
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\worker\facade;
|
||||
|
||||
use think\Facade;
|
||||
|
||||
/**
|
||||
* @see \think\worker\Worker
|
||||
* @mixin \think\worker\Worker
|
||||
* @method void option(array $option) static 参数设置
|
||||
* @method void start() static 启动服务
|
||||
* @method void stop() static 停止服务
|
||||
*/
|
||||
class Worker extends Facade
|
||||
{
|
||||
}
|
||||
276
Server/vendor/topthink/think-worker/src/log/File.php
vendored
Normal file
276
Server/vendor/topthink/think-worker/src/log/File.php
vendored
Normal file
@@ -0,0 +1,276 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\log\driver;
|
||||
|
||||
use think\App;
|
||||
|
||||
/**
|
||||
* 本地化调试输出到文件
|
||||
*/
|
||||
class File
|
||||
{
|
||||
protected $config = [
|
||||
'time_format' => ' c ',
|
||||
'single' => false,
|
||||
'file_size' => 2097152,
|
||||
'path' => '',
|
||||
'apart_level' => [],
|
||||
'max_files' => 0,
|
||||
'json' => false,
|
||||
];
|
||||
|
||||
protected $app;
|
||||
|
||||
// 实例化并传入参数
|
||||
public function __construct(App $app, $config = [])
|
||||
{
|
||||
$this->app = $app;
|
||||
|
||||
if (is_array($config)) {
|
||||
$this->config = array_merge($this->config, $config);
|
||||
}
|
||||
|
||||
if (empty($this->config['path'])) {
|
||||
$this->config['path'] = $this->app->getRuntimePath() . 'log' . DIRECTORY_SEPARATOR;
|
||||
} elseif (substr($this->config['path'], -1) != DIRECTORY_SEPARATOR) {
|
||||
$this->config['path'] .= DIRECTORY_SEPARATOR;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 日志写入接口
|
||||
* @access public
|
||||
* @param array $log 日志信息
|
||||
* @param bool $append 是否追加请求信息
|
||||
* @return bool
|
||||
*/
|
||||
public function save(array $log = [], $append = false)
|
||||
{
|
||||
$destination = $this->getMasterLogFile();
|
||||
|
||||
$path = dirname($destination);
|
||||
!is_dir($path) && mkdir($path, 0755, true);
|
||||
|
||||
$info = [];
|
||||
|
||||
foreach ($log as $type => $val) {
|
||||
|
||||
foreach ($val as $msg) {
|
||||
if (!is_string($msg)) {
|
||||
$msg = var_export($msg, true);
|
||||
}
|
||||
|
||||
$info[$type][] = $this->config['json'] ? $msg : '[ ' . $type . ' ] ' . $msg;
|
||||
}
|
||||
|
||||
if (!$this->config['json'] && (true === $this->config['apart_level'] || in_array($type, $this->config['apart_level']))) {
|
||||
// 独立记录的日志级别
|
||||
$filename = $this->getApartLevelFile($path, $type);
|
||||
|
||||
$this->write($info[$type], $filename, true, $append);
|
||||
|
||||
unset($info[$type]);
|
||||
}
|
||||
}
|
||||
|
||||
if ($info) {
|
||||
return $this->write($info, $destination, false, $append);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 日志写入
|
||||
* @access protected
|
||||
* @param array $message 日志信息
|
||||
* @param string $destination 日志文件
|
||||
* @param bool $apart 是否独立文件写入
|
||||
* @param bool $append 是否追加请求信息
|
||||
* @return bool
|
||||
*/
|
||||
protected function write($message, $destination, $apart = false, $append = false)
|
||||
{
|
||||
// 检测日志文件大小,超过配置大小则备份日志文件重新生成
|
||||
$this->checkLogSize($destination);
|
||||
|
||||
// 日志信息封装
|
||||
$info['timestamp'] = date($this->config['time_format']);
|
||||
|
||||
foreach ($message as $type => $msg) {
|
||||
$info[$type] = is_array($msg) ? implode("\r\n", $msg) : $msg;
|
||||
}
|
||||
|
||||
if (PHP_SAPI == 'cli') {
|
||||
$message = $this->parseCliLog($info);
|
||||
} else {
|
||||
// 添加调试日志
|
||||
$this->getDebugLog($info, $append, $apart);
|
||||
|
||||
$message = $this->parseLog($info);
|
||||
}
|
||||
|
||||
return error_log($message, 3, $destination);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取主日志文件名
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
protected function getMasterLogFile()
|
||||
{
|
||||
if ($this->config['single']) {
|
||||
$name = is_string($this->config['single']) ? $this->config['single'] : 'worker';
|
||||
|
||||
$destination = $this->config['path'] . $name . '.log';
|
||||
} else {
|
||||
if ($this->config['max_files']) {
|
||||
$filename = date('Ymd') . '_worker.log';
|
||||
$files = glob($this->config['path'] . '*.log');
|
||||
|
||||
try {
|
||||
if (count($files) > $this->config['max_files']) {
|
||||
unlink($files[0]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
}
|
||||
} else {
|
||||
$filename = date('Ym') . DIRECTORY_SEPARATOR . date('d') . '_worker.log';
|
||||
}
|
||||
|
||||
$destination = $this->config['path'] . $filename;
|
||||
}
|
||||
|
||||
return $destination;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取独立日志文件名
|
||||
* @access public
|
||||
* @param string $path 日志目录
|
||||
* @param string $type 日志类型
|
||||
* @return string
|
||||
*/
|
||||
protected function getApartLevelFile($path, $type)
|
||||
{
|
||||
if ($this->config['single']) {
|
||||
$name = is_string($this->config['single']) ? $this->config['single'] : 'worker';
|
||||
|
||||
$name .= '_' . $type;
|
||||
} elseif ($this->config['max_files']) {
|
||||
$name = date('Ymd') . '_' . $type . '_worker';
|
||||
} else {
|
||||
$name = date('d') . '_' . $type . '_worker';
|
||||
}
|
||||
|
||||
return $path . DIRECTORY_SEPARATOR . $name . '.log';
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查日志文件大小并自动生成备份文件
|
||||
* @access protected
|
||||
* @param string $destination 日志文件
|
||||
* @return void
|
||||
*/
|
||||
protected function checkLogSize($destination)
|
||||
{
|
||||
if (is_file($destination) && floor($this->config['file_size']) <= filesize($destination)) {
|
||||
try {
|
||||
rename($destination, dirname($destination) . DIRECTORY_SEPARATOR . time() . '-' . basename($destination));
|
||||
} catch (\Exception $e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* CLI日志解析
|
||||
* @access protected
|
||||
* @param array $info 日志信息
|
||||
* @return string
|
||||
*/
|
||||
protected function parseCliLog($info)
|
||||
{
|
||||
if ($this->config['json']) {
|
||||
$message = json_encode($info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\r\n";
|
||||
} else {
|
||||
$now = $info['timestamp'];
|
||||
unset($info['timestamp']);
|
||||
|
||||
$message = implode("\r\n", $info);
|
||||
|
||||
$message = "[{$now}]" . $message . "\r\n";
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析日志
|
||||
* @access protected
|
||||
* @param array $info 日志信息
|
||||
* @return string
|
||||
*/
|
||||
protected function parseLog($info)
|
||||
{
|
||||
$requestInfo = [
|
||||
'ip' => $this->app['request']->ip(),
|
||||
'method' => $this->app['request']->method(),
|
||||
'host' => $this->app['request']->host(),
|
||||
'uri' => $this->app['request']->url(),
|
||||
];
|
||||
|
||||
if ($this->config['json']) {
|
||||
$info = $requestInfo + $info;
|
||||
return json_encode($info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\r\n";
|
||||
}
|
||||
|
||||
array_unshift($info, "---------------------------------------------------------------\r\n[{$info['timestamp']}] {$requestInfo['ip']} {$requestInfo['method']} {$requestInfo['host']}{$requestInfo['uri']}");
|
||||
unset($info['timestamp']);
|
||||
|
||||
return implode("\r\n", $info) . "\r\n";
|
||||
}
|
||||
|
||||
protected function getDebugLog(&$info, $append, $apart)
|
||||
{
|
||||
if ($this->app->isDebug() && $append) {
|
||||
|
||||
if ($this->config['json']) {
|
||||
// 获取基本信息
|
||||
$runtime = round(microtime(true) - $this->app->getBeginTime(), 10);
|
||||
$reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
|
||||
|
||||
$memory_use = number_format((memory_get_usage() - $this->app->getBeginMem()) / 1024, 2);
|
||||
|
||||
$info = [
|
||||
'runtime' => number_format($runtime, 6) . 's',
|
||||
'reqs' => $reqs . 'req/s',
|
||||
'memory' => $memory_use . 'kb',
|
||||
'file' => count(get_included_files()),
|
||||
] + $info;
|
||||
|
||||
} elseif (!$apart) {
|
||||
// 增加额外的调试信息
|
||||
$runtime = round(microtime(true) - $this->app->getBeginTime(), 10);
|
||||
$reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
|
||||
|
||||
$memory_use = number_format((memory_get_usage() - $this->app->getBeginMem()) / 1024, 2);
|
||||
|
||||
$time_str = '[运行时间:' . number_format($runtime, 6) . 's] [吞吐率:' . $reqs . 'req/s]';
|
||||
$memory_str = ' [内存消耗:' . $memory_use . 'kb]';
|
||||
$file_load = ' [文件加载:' . count(get_included_files()) . ']';
|
||||
|
||||
array_unshift($info, $time_str . $memory_str . $file_load);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user