From 0be38966efadce0aad8674d8435bc0e4fa42c311 Mon Sep 17 00:00:00 2001 From: smalleyes Date: Fri, 18 May 2018 09:09:11 +0800 Subject: [PATCH 01/11] Add start and restart output information. --- src/Commands/HttpServerCommand.php | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Commands/HttpServerCommand.php b/src/Commands/HttpServerCommand.php index 1703a469..46cf5b5f 100644 --- a/src/Commands/HttpServerCommand.php +++ b/src/Commands/HttpServerCommand.php @@ -89,9 +89,10 @@ protected function start() $this->info("Swoole http server started: "); if ($this->isDaemon()) { $this->info('> (You can run this command to ensure the ' . - 'swoole_http_server process is running: ps aux|grep "swoole")'); + 'swoole_http_server process is running: ps aux|grep "swoole")'); } + $this->showInfos(); $this->laravel->make('swoole.http')->run(); } @@ -161,38 +162,51 @@ protected function reload() $this->info('> success'); } - /** * Display PHP and Swoole misc info. */ protected function infos() { - $this->showInfos(); + $this->showInfos(true); } /** * Display PHP and Swoole miscs infos. + * + * @param bool $more */ - protected function showInfos() + protected function showInfos($more=false) { $pid = $this->getPid(); $isRunning = $this->isRunning($pid); $host = $this->configs['server']['host']; $port = $this->configs['server']['port']; + $reactorNum = $this->configs['server']['options']['reactor_num']; + $workerNum = $this->configs['server']['options']['worker_num']; + $taskWorkerNum = $this->configs['server']['options']['task_worker_num']; $isWebsocket = $this->configs['websocket']['enabled']; $logFile = $this->configs['server']['options']['log_file']; - $this->table(['Name', 'Value'], [ + $table = [ ['PHP Version', 'Version' => phpversion()], ['Swoole Version', 'Version' => swoole_version()], ['Laravel Version', $this->getApplication()->getVersion()], - ['Server Status', $isRunning ? 'Online' : 'Offline'], ['Listen IP', $host], ['Listen Port', $port], + ['Reactor Num', $reactorNum], + ['Worker Num', $workerNum], + ['Task Worker Num', $taskWorkerNum], ['Websocket Mode', $isWebsocket ? 'On' : 'Off'], - ['PID', $isRunning ? $pid : 'None'], ['Log Path', $logFile], - ]); + ]; + if ($more) { + $table = array_merge($table, [ + ['Server Status', $isRunning ? 'Online' : 'Offline'], + ['PID', $isRunning ? $pid : 'None'], + ]); + } + + $this->table(['Name', 'Value'], $table); } /** From 4968b10b5a407579be51d4e70bbeeb9f5ee36d1e Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Sat, 19 May 2018 18:05:22 +0800 Subject: [PATCH 02/11] fix Task Worker Num shown on infos and remove display while start --- src/Commands/HttpServerCommand.php | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/Commands/HttpServerCommand.php b/src/Commands/HttpServerCommand.php index 46cf5b5f..21aa4813 100644 --- a/src/Commands/HttpServerCommand.php +++ b/src/Commands/HttpServerCommand.php @@ -92,7 +92,6 @@ protected function start() 'swoole_http_server process is running: ps aux|grep "swoole")'); } - $this->showInfos(); $this->laravel->make('swoole.http')->run(); } @@ -167,7 +166,7 @@ protected function reload() */ protected function infos() { - $this->showInfos(true); + $this->showInfos(); } /** @@ -175,7 +174,7 @@ protected function infos() * * @param bool $more */ - protected function showInfos($more=false) + protected function showInfos() { $pid = $this->getPid(); $isRunning = $this->isRunning($pid); @@ -193,18 +192,14 @@ protected function showInfos($more=false) ['Laravel Version', $this->getApplication()->getVersion()], ['Listen IP', $host], ['Listen Port', $port], + ['Server Status', $isRunning ? 'Online' : 'Offline'], ['Reactor Num', $reactorNum], ['Worker Num', $workerNum], - ['Task Worker Num', $taskWorkerNum], + ['Task Worker Num', $isWebsocket ? $taskWorkerNum : 0], ['Websocket Mode', $isWebsocket ? 'On' : 'Off'], + ['PID', $isRunning ? $pid : 'None'], ['Log Path', $logFile], ]; - if ($more) { - $table = array_merge($table, [ - ['Server Status', $isRunning ? 'Online' : 'Offline'], - ['PID', $isRunning ? $pid : 'None'], - ]); - } $this->table(['Name', 'Value'], $table); } From 579854aa057c486e887a7afccd8ebaf1e3788a10 Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Sat, 19 May 2018 22:05:15 +0800 Subject: [PATCH 03/11] add a mock swoole_cpu_num function for config --- composer.json | 3 +++ src/Server/helpers.php | 11 +++++++++++ 2 files changed, 14 insertions(+) create mode 100644 src/Server/helpers.php diff --git a/composer.json b/composer.json index fefe47d5..1c3508ff 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,9 @@ "codedungeon/phpunit-result-printer": "^0.14.0" }, "autoload": { + "files": [ + "src/Server/helpers.php", + ], "psr-4": { "SwooleTW\\Http\\": "src" } diff --git a/src/Server/helpers.php b/src/Server/helpers.php new file mode 100644 index 00000000..95c40146 --- /dev/null +++ b/src/Server/helpers.php @@ -0,0 +1,11 @@ + Date: Sat, 19 May 2018 22:12:45 +0800 Subject: [PATCH 04/11] fix json syntax in composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1c3508ff..3299fb43 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ }, "autoload": { "files": [ - "src/Server/helpers.php", + "src/Server/helpers.php" ], "psr-4": { "SwooleTW\\Http\\": "src" From 76203bfa7bfa601b5649baf115b131ed62166208 Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Sat, 19 May 2018 23:02:36 +0800 Subject: [PATCH 05/11] leave room after calling disconnect callback in websocket --- src/Websocket/CanWebsocket.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Websocket/CanWebsocket.php b/src/Websocket/CanWebsocket.php index 8fdcc18b..42760ea5 100644 --- a/src/Websocket/CanWebsocket.php +++ b/src/Websocket/CanWebsocket.php @@ -133,14 +133,15 @@ public function onClose(Server $server, $fd, $reactorId) } try { - // leave all rooms - $this->websocket->reset(true)->setSender($fd)->leave(); + $this->websocket->reset(true)->setSender($fd); // trigger 'disconnect' websocket event if ($this->websocket->eventExists('disconnect')) { $this->websocket->call('disconnect'); } else { $this->websocketHandler->onClose($fd, $reactorId); } + // leave all rooms + $this->websocket->leave(); } catch (Exception $e) { $this->logServerError($e); } From 9bebcc9b400f777c09d0f8c270bc8ecc84b1aa9f Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Sun, 20 May 2018 00:13:47 +0800 Subject: [PATCH 06/11] only enable task worker in websocket mode --- src/Server/Manager.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Server/Manager.php b/src/Server/Manager.php index 5795cadc..62f3310e 100644 --- a/src/Server/Manager.php +++ b/src/Server/Manager.php @@ -154,6 +154,11 @@ protected function configureSwooleServer() { $config = $this->container['config']->get('swoole_http.server.options'); + // only enable task worker in websocket mode + if (! $this->isWebsocket) { + unset($config['task_worker_num']); + } + $this->server->set($config); } From 0159eba9ebc118e3389b758097b07e44578f3d28 Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Tue, 22 May 2018 21:34:00 +0800 Subject: [PATCH 07/11] add isUserIdOnline and logout apis for websocket --- src/Websocket/Authenticatable.php | 20 ++++++++++++++++++++ src/Websocket/Websocket.php | 2 +- tests/Websocket/WebsocketTest.php | 27 +++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/Websocket/Authenticatable.php b/src/Websocket/Authenticatable.php index b20d754a..ae9d1c3d 100644 --- a/src/Websocket/Authenticatable.php +++ b/src/Websocket/Authenticatable.php @@ -25,6 +25,18 @@ public function loginUsingId($userId) return $this->join(static::USER_PREFIX . $userId); } + /** + * Logout with current sender's fd. + */ + public function logout() + { + if (is_null($userId = $this->getUserId())) { + return; + } + + return $this->leave(static::USER_PREFIX . $userId); + } + /** * Set multiple recepients' fds by users. */ @@ -75,6 +87,14 @@ public function getUserId() return $this->userId; } + /** + * Check if a user is online by given userId. + */ + public function isUserIdOnline($userId) + { + return ! empty($this->room->getClients(static::USER_PREFIX . $userId)); + } + /** * Check if user object implements AuthenticatableContract. */ diff --git a/src/Websocket/Websocket.php b/src/Websocket/Websocket.php index 8e5f2f96..0b72a732 100644 --- a/src/Websocket/Websocket.php +++ b/src/Websocket/Websocket.php @@ -296,7 +296,7 @@ protected function getFds() foreach ($rooms as $room) { $clients = $this->room->getClients($room); - // rollback fd with wrong type back to fds array + // fallback fd with wrong type back to fds array if (empty($clients) && is_numeric($room)) { $fds[] = $room; } else { diff --git a/tests/Websocket/WebsocketTest.php b/tests/Websocket/WebsocketTest.php index 8f06d85f..14b3a200 100644 --- a/tests/Websocket/WebsocketTest.php +++ b/tests/Websocket/WebsocketTest.php @@ -205,6 +205,33 @@ public function testGetUserId() $this->assertEquals($sender, $websocket->getUserId()); } + public function testLogout() + { + $room = m::mock(RoomContract::class); + $room->shouldReceive('getRooms') + ->with($sender = 1) + ->once() + ->andReturn(['uid_1']); + $room->shouldReceive('delete') + ->with($sender, $name = ['uid_1']) + ->once(); + + $websocket = $this->getWebsocket($room)->setSender($sender); + $websocket->logout(); + } + + public function testIsUserIdOnline() + { + $room = m::mock(RoomContract::class); + $room->shouldReceive('getClients') + ->with('uid_1') + ->once() + ->andReturn([1]); + + $websocket = $this->getWebsocket($room); + $this->assertTrue($websocket->isUserIdOnline(1)); + } + public function testReset() { $websocket = $this->getWebsocket(); From ea8bb7f6aa77a298f5d4d0c1639ef3a5d498721f Mon Sep 17 00:00:00 2001 From: smalleyes Date: Wed, 23 May 2018 11:49:18 +0800 Subject: [PATCH 08/11] fix wording ,fix resetCookie in sendbox, fix delete pidfile --- src/Server/Manager.php | 16 ++++++++++------ src/Server/Sandbox.php | 3 ++- src/Table/SwooleTable.php | 1 + 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Server/Manager.php b/src/Server/Manager.php index 5795cadc..4780a7c8 100644 --- a/src/Server/Manager.php +++ b/src/Server/Manager.php @@ -246,7 +246,7 @@ public function onRequest($swooleRequest, $swooleResponse) return; } - // set currnt request to sandbox + // set current request to sandbox $this->sandbox->setRequest($illuminateRequest); // enable sandbox @@ -257,9 +257,6 @@ public function onRequest($swooleRequest, $swooleResponse) $illuminateResponse = $application->run($illuminateRequest); $response = Response::make($illuminateResponse, $swooleResponse); $response->send(); - - // disable and recycle sandbox resource - $this->sandbox->disable(); } catch (Exception $e) { try { $exceptionResponse = $this->app[ExceptionHandler::class]->render($illuminateRequest, $e); @@ -268,6 +265,9 @@ public function onRequest($swooleRequest, $swooleResponse) } catch (Exception $e) { $this->logServerError($e); } + } finally { + // disable and recycle sandbox resource + $this->sandbox->disable(); } } @@ -276,6 +276,7 @@ public function onRequest($swooleRequest, $swooleResponse) * * @param \Illuminate\Http\Request $illuminateRequest * @param \Swoole\Http\Response $swooleResponse + * @return boolean */ protected function handleStaticRequest($illuminateRequest, $swooleResponse) { @@ -320,7 +321,7 @@ protected function resetOnRequest() /** * Set onTask listener. */ - public function onTask(HttpServer $server, $taskId, $fromId, $data) + public function onTask(HttpServer $server, $taskId, $srcWorkerId, $data) { $this->container['events']->fire('swoole.task', func_get_args()); @@ -440,7 +441,10 @@ protected function createPidFile() */ protected function removePidFile() { - unlink($this->getPidFile()); + $pidFile = $this->getPidFile(); + if (file_exists($pidFile)) { + unlink($pidFile); + } } /** diff --git a/src/Server/Sandbox.php b/src/Server/Sandbox.php index 2c426652..4e26a6a2 100644 --- a/src/Server/Sandbox.php +++ b/src/Server/Sandbox.php @@ -213,7 +213,8 @@ protected function resetSession($application) protected function resetCookie($application) { if (isset($application['cookie'])) { - foreach ($application->make('cookie')->getQueuedCookies() as $key => $value) { + $cookies = $application->make('cookie'); + foreach ($cookies->getQueuedCookies() as $key => $value) { $cookies->unqueue($key); } } diff --git a/src/Table/SwooleTable.php b/src/Table/SwooleTable.php index 4bdf4d6c..9dfe8947 100644 --- a/src/Table/SwooleTable.php +++ b/src/Table/SwooleTable.php @@ -18,6 +18,7 @@ class SwooleTable * * @param string $name * @param \Swoole\Table $table + * @return \SwooleTW\Http\Table\SwooleTable */ public function add(string $name, Table $table) { From 3e4de4450d890a6c7b496576e8b71322156e220a Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Sat, 26 May 2018 15:43:04 +0800 Subject: [PATCH 09/11] require socket.io routes only when websocket is enabled --- src/HttpServiceProvider.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/HttpServiceProvider.php b/src/HttpServiceProvider.php index 394b079f..4d43977d 100644 --- a/src/HttpServiceProvider.php +++ b/src/HttpServiceProvider.php @@ -52,7 +52,10 @@ public function boot() __DIR__ . '/../config/swoole_websocket.php' => base_path('config/swoole_websocket.php'), __DIR__ . '/../routes/websocket.php' => base_path('routes/websocket.php') ], 'laravel-swoole'); - $this->bootRoutes(); + + if ($this->app['config']->get('swoole_http.websocket.enabled')) { + $this->bootRoutes(); + } } /** From e407e84f51aea170cde1bdb7daf32e8233339e1e Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Sat, 26 May 2018 15:45:55 +0800 Subject: [PATCH 10/11] disable sandbox in finally --- src/Websocket/CanWebsocket.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Websocket/CanWebsocket.php b/src/Websocket/CanWebsocket.php index 42760ea5..68a024c0 100644 --- a/src/Websocket/CanWebsocket.php +++ b/src/Websocket/CanWebsocket.php @@ -74,10 +74,11 @@ public function onOpen(Server $server, $swooleRequest) $this->websocket->setContainer($application); $this->websocket->call('connect', $illuminateRequest); } - // disable and recycle sandbox resource - $this->sandbox->disable(); } catch (Exception $e) { $this->logServerError($e); + } finally { + // disable and recycle sandbox resource + $this->sandbox->disable(); } } @@ -112,10 +113,11 @@ public function onMessage(Server $server, Frame $frame) } else { $this->websocketHandler->onMessage($frame); } - // disable and recycle sandbox resource - $this->sandbox->disable(); } catch (Exception $e) { $this->logServerError($e); + } finally { + // disable and recycle sandbox resource + $this->sandbox->disable(); } } From 2b8f3e0a98da597bb0bfad38f3dc173de3adabe8 Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Sat, 26 May 2018 21:53:39 +0800 Subject: [PATCH 11/11] add blank line --- src/Server/Manager.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Server/Manager.php b/src/Server/Manager.php index c393db9b..ebab2047 100644 --- a/src/Server/Manager.php +++ b/src/Server/Manager.php @@ -447,6 +447,7 @@ protected function createPidFile() protected function removePidFile() { $pidFile = $this->getPidFile(); + if (file_exists($pidFile)) { unlink($pidFile); }