├── LICENSE ├── README.md ├── composer.json └── src ├── BusinessWorker.php ├── Gateway.php ├── Install.php ├── Register.php ├── config └── plugin │ └── webman │ └── gateway-worker │ ├── app.php │ └── process.php └── plugin └── webman └── gateway └── Events.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 webman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gateway-worker 2 | webman gateway-worker plugin 3 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "webman/gateway-worker", 3 | "type": "library", 4 | "license": "MIT", 5 | "require": { 6 | "workerman/gateway-worker": ">=3.0" 7 | }, 8 | "autoload": { 9 | "psr-4": { 10 | "Webman\\GatewayWorker\\": "src" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/BusinessWorker.php: -------------------------------------------------------------------------------- 1 | $value) 9 | { 10 | $this->$key = $value; 11 | } 12 | } 13 | 14 | public function onWorkerStart() 15 | { 16 | $this->_onWorkerStart = $this->onWorkerStart; 17 | $this->_onWorkerReload = $this->onWorkerReload; 18 | $this->_onWorkerStop = $this->onWorkerStop; 19 | $this->onWorkerStop = array($this, 'onWorkerStop'); 20 | $this->onWorkerStart = array($this, 'onWorkerStart'); 21 | $this->onWorkerReload = array($this, 'onWorkerReload'); 22 | 23 | $args = func_get_args(); 24 | $this->id = $args[0]->id; 25 | parent::onWorkerStart(); 26 | } 27 | } -------------------------------------------------------------------------------- /src/Gateway.php: -------------------------------------------------------------------------------- 1 | $value) 14 | { 15 | $this->$key = $value; 16 | } 17 | 18 | $this->router = array("\\GatewayWorker\\Gateway", 'routerBind'); 19 | } 20 | 21 | public function onConnect($connection) 22 | { 23 | parent::onClientConnect($connection); 24 | } 25 | 26 | public function onClose($connection) 27 | { 28 | parent::onClientClose($connection); 29 | } 30 | 31 | public function onMessage($connection, $data) 32 | { 33 | parent::onClientMessage($connection, $data); 34 | } 35 | 36 | public function onWorkerStart() 37 | { 38 | // 保存用户的回调,当对应的事件发生时触发 39 | $this->_onWorkerStart = $this->onWorkerStart; 40 | $this->onWorkerStart = array($this, 'onWorkerStart'); 41 | // 保存用户的回调,当对应的事件发生时触发 42 | $this->_onConnect = $this->onConnect; 43 | $this->onConnect = array($this, 'onClientConnect'); 44 | 45 | // onMessage禁止用户设置回调 46 | $this->onMessage = array($this, 'onClientMessage'); 47 | 48 | // 保存用户的回调,当对应的事件发生时触发 49 | $this->_onClose = $this->onClose; 50 | $this->onClose = array($this, 'onClientClose'); 51 | // 保存用户的回调,当对应的事件发生时触发 52 | $this->_onWorkerStop = $this->onWorkerStop; 53 | $this->onWorkerStop = array($this, 'onWorkerStop'); 54 | 55 | if (!is_array($this->registerAddress)) { 56 | $this->registerAddress = array($this->registerAddress); 57 | } 58 | 59 | // 记录进程启动的时间 60 | $this->_startTime = time(); 61 | 62 | $args = func_get_args(); 63 | $this->id = $args[0]->id; 64 | 65 | $worker = func_get_arg(0); 66 | $this->_gatewayPort = substr(strrchr($worker->getSocketName(),':'),1); 67 | 68 | parent::onWorkerStart(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Install.php: -------------------------------------------------------------------------------- 1 | 'plugin/webman/gateway', 13 | 'config/plugin/webman/gateway-worker' => 'config/plugin/webman/gateway-worker', 14 | ); 15 | 16 | /** 17 | * Install 18 | * @return void 19 | */ 20 | public static function install() 21 | { 22 | static::installByRelation(); 23 | } 24 | 25 | /** 26 | * Uninstall 27 | * @return void 28 | */ 29 | public static function uninstall() 30 | { 31 | self::uninstallByRelation(); 32 | } 33 | 34 | /** 35 | * installByRelation 36 | * @return void 37 | */ 38 | public static function installByRelation() 39 | { 40 | foreach (static::$pathRelation as $source => $dest) { 41 | if ($pos = strrpos($dest, '/')) { 42 | $parent_dir = base_path().'/'.substr($dest, 0, $pos); 43 | if (!is_dir($parent_dir)) { 44 | mkdir($parent_dir, 0777, true); 45 | } 46 | } 47 | //symlink(__DIR__ . "/$source", base_path()."/$dest"); 48 | copy_dir(__DIR__ . "/$source", base_path()."/$dest"); 49 | } 50 | } 51 | 52 | /** 53 | * uninstallByRelation 54 | * @return void 55 | */ 56 | public static function uninstallByRelation() 57 | { 58 | foreach (static::$pathRelation as $source => $dest) { 59 | $path = base_path()."/$dest"; 60 | if (!is_dir($path) && !is_file($path)) { 61 | continue; 62 | } 63 | /*if (is_link($path) { 64 | unlink($path); 65 | }*/ 66 | remove_dir($path); 67 | } 68 | } 69 | 70 | } -------------------------------------------------------------------------------- /src/Register.php: -------------------------------------------------------------------------------- 1 | $property = $config[$property]; 17 | } 18 | } 19 | } 20 | 21 | public function onWorkerstart() 22 | { 23 | // 设置 onMessage 连接回调 24 | $this->onConnect = array($this, 'onConnect'); 25 | 26 | // 设置 onMessage 回调 27 | $this->onMessage = array($this, 'onMessage'); 28 | 29 | // 设置 onClose 回调 30 | $this->onClose = array($this, 'onClose'); 31 | 32 | // 记录进程启动的时间 33 | $this->_startTime = time(); 34 | 35 | // 强制使用text协议 36 | $this->protocol = '\Workerman\Protocols\Text'; 37 | 38 | // reusePort 39 | $this->reusePort = false; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/config/plugin/webman/gateway-worker/app.php: -------------------------------------------------------------------------------- 1 | true, 4 | ]; -------------------------------------------------------------------------------- /src/config/plugin/webman/gateway-worker/process.php: -------------------------------------------------------------------------------- 1 | [ 9 | 'handler' => Gateway::class, 10 | 'listen' => 'websocket://0.0.0.0:7272', 11 | 'count' => 2, 12 | 'reloadable' => false, 13 | 'constructor' => ['config' => [ 14 | 'lanIp' => '127.0.0.1', 15 | 'startPort' => 2300, 16 | 'pingInterval' => 25, 17 | 'pingData' => '{"type":"ping"}', 18 | 'registerAddress' => '127.0.0.1:1236', 19 | 'onConnect' => function(){}, 20 | ]] 21 | ], 22 | 'worker' => [ 23 | 'handler' => BusinessWorker::class, 24 | 'count' => cpu_count()*2, 25 | 'constructor' => ['config' => [ 26 | 'eventHandler' => plugin\webman\gateway\Events::class, 27 | 'name' => 'ChatBusinessWorker', 28 | 'registerAddress' => '127.0.0.1:1236', 29 | ]] 30 | ], 31 | 'register' => [ 32 | 'handler' => Register::class, 33 | 'listen' => 'text://127.0.0.1:1236', 34 | 'count' => 1, // Must be 1 35 | 'reloadable' => false, 36 | 'constructor' => [] 37 | ], 38 | ]; 39 | -------------------------------------------------------------------------------- /src/plugin/webman/gateway/Events.php: -------------------------------------------------------------------------------- 1 |