From 3bf132ba410ad8ea0b41ccb21542c1c7714ada56 Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Sat, 12 May 2018 21:22:26 +0800 Subject: [PATCH 1/7] fix `disconnect` event name in example websocket route file --- routes/websocket.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/websocket.php b/routes/websocket.php index bc8beb9d..01ccf392 100644 --- a/routes/websocket.php +++ b/routes/websocket.php @@ -16,7 +16,7 @@ // called while socket on connect }); -Websocket::on('close', function ($websocket) { +Websocket::on('disconnect', function ($websocket) { // called while socket on close }); From 1f3db9d29d57c9689035999127e16ffeaeececa8 Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Sat, 12 May 2018 21:56:27 +0800 Subject: [PATCH 2/7] fix wording --- config/swoole_http.php | 2 +- routes/websocket.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/swoole_http.php b/config/swoole_http.php index 6d22e032..97bc750b 100644 --- a/config/swoole_http.php +++ b/config/swoole_http.php @@ -35,7 +35,7 @@ 'max_request' => 3000, // Enable coroutine send 'send_yield' => true, - // You must --enable-openssl while compiling Swoole + // You must add --enable-openssl while compiling Swoole 'ssl_cert_file' => null, 'ssl_key_file' => null, ], diff --git a/routes/websocket.php b/routes/websocket.php index 01ccf392..a956ac7d 100644 --- a/routes/websocket.php +++ b/routes/websocket.php @@ -17,7 +17,7 @@ }); Websocket::on('disconnect', function ($websocket) { - // called while socket on close + // called while socket on disconnect }); Websocket::on('example', function ($websocket, $data) { From c012b02eb33fdea03c9cff9000470a6e02738315 Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Sun, 13 May 2018 14:47:10 +0800 Subject: [PATCH 3/7] fix comment wording --- config/swoole_http.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/swoole_http.php b/config/swoole_http.php index 97bc750b..59d77c7a 100644 --- a/config/swoole_http.php +++ b/config/swoole_http.php @@ -15,7 +15,7 @@ 'host' => env('SWOOLE_HTTP_HOST', '127.0.0.1'), 'port' => env('SWOOLE_HTTP_PORT', '1215'), 'public_path' => base_path('public'), - // If use swoole to respond request for static files + // Determine if to use swoole to respond request for static files 'handle_static_files' => true, 'options' => [ 'pid_file' => env('SWOOLE_HTTP_PID_FILE', base_path('storage/logs/swoole_http.pid')), From ceedc6d6d13c121521838406b071df8b2ccbf692 Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Sun, 13 May 2018 14:57:59 +0800 Subject: [PATCH 4/7] add SWOOLE_HANDLE_STATIC env --- config/swoole_http.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/swoole_http.php b/config/swoole_http.php index 59d77c7a..ebb26156 100644 --- a/config/swoole_http.php +++ b/config/swoole_http.php @@ -16,7 +16,7 @@ 'port' => env('SWOOLE_HTTP_PORT', '1215'), 'public_path' => base_path('public'), // Determine if to use swoole to respond request for static files - 'handle_static_files' => true, + 'handle_static_files' => env('SWOOLE_HANDLE_STATIC', true), 'options' => [ 'pid_file' => env('SWOOLE_HTTP_PID_FILE', base_path('storage/logs/swoole_http.pid')), 'log_file' => env('SWOOLE_HTTP_LOG_FILE', base_path('storage/logs/swoole_http.log')), From 31462ac2693729816dbe7384bd8b99a9da998365 Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Sun, 13 May 2018 21:24:19 +0800 Subject: [PATCH 5/7] apply sandbox to onMessage for websocket --- src/Websocket/CanWebsocket.php | 42 ++++++++++++++-------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/src/Websocket/CanWebsocket.php b/src/Websocket/CanWebsocket.php index d5ad7fab..8fdcc18b 100644 --- a/src/Websocket/CanWebsocket.php +++ b/src/Websocket/CanWebsocket.php @@ -58,15 +58,24 @@ public function onOpen(Server $server, $swooleRequest) $illuminateRequest = Request::make($swooleRequest)->toIlluminate(); try { + $this->websocket->reset(true)->setSender($swooleRequest->fd); + // set currnt request to sandbox + $this->sandbox->setRequest($illuminateRequest); + // enable sandbox + $application = $this->sandbox->getLaravelApp(); + $this->sandbox->enable(); // check if socket.io connection established if (! $this->websocketHandler->onOpen($swooleRequest->fd, $illuminateRequest)) { return; } - $this->websocket->reset(true)->setSender($swooleRequest->fd); // trigger 'connect' websocket event if ($this->websocket->eventExists('connect')) { - $this->callOnConnect($illuminateRequest); + // set sandbox container to websocket pipeline + $this->websocket->setContainer($application); + $this->websocket->call('connect', $illuminateRequest); } + // disable and recycle sandbox resource + $this->sandbox->disable(); } catch (Exception $e) { $this->logServerError($e); } @@ -93,12 +102,18 @@ public function onMessage(Server $server, Frame $frame) $this->websocket->reset(true)->setSender($frame->fd); + // enable sandbox + $application = $this->sandbox->getLaravelApp(); + $this->sandbox->enable(); + // dispatch message to registered event callback if ($this->websocket->eventExists($payload['event'])) { $this->websocket->call($payload['event'], $payload['data']); } else { $this->websocketHandler->onMessage($frame); } + // disable and recycle sandbox resource + $this->sandbox->disable(); } catch (Exception $e) { $this->logServerError($e); } @@ -279,27 +294,4 @@ protected function normalizePushData(array $data) return [$opcode, $sender, $fds, $broadcast, $assigned, $event, $message]; } - - /** - * Call on connect event callback . - */ - protected function callOnConnect($illuminateRequest) - { - // set currnt request to sandbox - $this->sandbox->setRequest($illuminateRequest); - - // get application from sandbox - $application = $this->sandbox->getLaravelApp(); - - // reset session - if (isset($application['session'])) { - $application['session']->flush(); - } - - // set sandbox container to websocket pipeline - $this->websocket->setContainer($application); - $this->sandbox->enable(); - $this->websocket->call('connect', $illuminateRequest); - $this->sandbox->disable(); - } } From c03c2fd8c1e8caf8a5bb748737ed7104c830e6b5 Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Sun, 13 May 2018 22:05:21 +0800 Subject: [PATCH 6/7] add getUserId and getUser functions to websocket --- src/Websocket/Authenticatable.php | 40 +++++++++++++++++++++++++++++++ src/Websocket/Websocket.php | 2 ++ tests/Websocket/WebsocketTest.php | 31 ++++++++++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/src/Websocket/Authenticatable.php b/src/Websocket/Authenticatable.php index eb33f92c..fa54156b 100644 --- a/src/Websocket/Authenticatable.php +++ b/src/Websocket/Authenticatable.php @@ -3,10 +3,14 @@ namespace SwooleTW\Http\Websocket; use InvalidArgumentException; +use Illuminate\Support\Facades\Auth; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; trait Authenticatable { + protected $user; + protected $userId; + /** * Login using current user. */ @@ -53,6 +57,42 @@ public function toUserId($userIds) return $this; } + /** + * Get current auth user by sender's fd. + */ + public function getUser() + { + if ($this->user instanceof AuthenticatableContract) { + return $this->user; + } + + if (! is_null($uid = $this->getUserId()) && $user = Auth::loginUsingId($uid)) { + $this->user = $user; + } + + return $this->user; + } + + /** + * Get current auth user id by sender's fd. + */ + public function getUserId() + { + if (! is_null($this->userId)) { + return $this->userId; + } + + $rooms = $this->room->getRooms($this->getSender()); + + foreach ($rooms as $room) { + if (count($explode = explode(static::USER_PREFIX, $room)) === 2) { + $this->userId = $explode[1]; + } + } + + return $this->userId; + } + /** * Check if user object implements AuthenticatableContract. */ diff --git a/src/Websocket/Websocket.php b/src/Websocket/Websocket.php index 0d44241c..c36f9523 100644 --- a/src/Websocket/Websocket.php +++ b/src/Websocket/Websocket.php @@ -317,6 +317,8 @@ public function reset($force = false) if ($force) { $this->sender = null; + $this->user = null; + $this->userId = null; } return $this; diff --git a/tests/Websocket/WebsocketTest.php b/tests/Websocket/WebsocketTest.php index 78fedfa1..77037971 100644 --- a/tests/Websocket/WebsocketTest.php +++ b/tests/Websocket/WebsocketTest.php @@ -7,6 +7,7 @@ use InvalidArgumentException; use Illuminate\Pipeline\Pipeline; use SwooleTW\Http\Tests\TestCase; +use Illuminate\Support\Facades\Auth; use SwooleTW\Http\Websocket\Websocket; use SwooleTW\Http\Websocket\Rooms\RoomContract; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; @@ -193,6 +194,36 @@ public function testToUser() $this->assertTrue(in_array(3, $websocket->getTo())); } + public function testGetUserId() + { + $room = m::mock(RoomContract::class); + $room->shouldReceive('getRooms') + ->with($sender = 1) + ->once() + ->andReturn(['uid_1']); + + $websocket = $this->getWebsocket($room)->setSender($sender); + $this->assertEquals($sender, $websocket->getUserId()); + } + + public function testGetUser() + { + $room = m::mock(RoomContract::class); + $room->shouldReceive('getRooms') + ->with($sender = 1) + ->once() + ->andReturn(['uid_1']); + + $user = m::mock(AuthenticatableContract::class); + Auth::shouldReceive('loginUsingId') + ->with($sender) + ->once() + ->andReturn($user); + + $websocket = $this->getWebsocket($room)->setSender($sender); + $this->assertEquals($user, $websocket->getUser()); + } + public function testReset() { $websocket = $this->getWebsocket(); From 106ec248ca7cc540951740463d41a89a44ab8149 Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Sun, 13 May 2018 22:24:59 +0800 Subject: [PATCH 7/7] remove getUser from websocket --- src/Websocket/Authenticatable.php | 18 ------------------ src/Websocket/Websocket.php | 1 - tests/Websocket/WebsocketTest.php | 19 ------------------- 3 files changed, 38 deletions(-) diff --git a/src/Websocket/Authenticatable.php b/src/Websocket/Authenticatable.php index fa54156b..b20d754a 100644 --- a/src/Websocket/Authenticatable.php +++ b/src/Websocket/Authenticatable.php @@ -3,12 +3,10 @@ namespace SwooleTW\Http\Websocket; use InvalidArgumentException; -use Illuminate\Support\Facades\Auth; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; trait Authenticatable { - protected $user; protected $userId; /** @@ -57,22 +55,6 @@ public function toUserId($userIds) return $this; } - /** - * Get current auth user by sender's fd. - */ - public function getUser() - { - if ($this->user instanceof AuthenticatableContract) { - return $this->user; - } - - if (! is_null($uid = $this->getUserId()) && $user = Auth::loginUsingId($uid)) { - $this->user = $user; - } - - return $this->user; - } - /** * Get current auth user id by sender's fd. */ diff --git a/src/Websocket/Websocket.php b/src/Websocket/Websocket.php index c36f9523..8e5f2f96 100644 --- a/src/Websocket/Websocket.php +++ b/src/Websocket/Websocket.php @@ -317,7 +317,6 @@ public function reset($force = false) if ($force) { $this->sender = null; - $this->user = null; $this->userId = null; } diff --git a/tests/Websocket/WebsocketTest.php b/tests/Websocket/WebsocketTest.php index 77037971..8f06d85f 100644 --- a/tests/Websocket/WebsocketTest.php +++ b/tests/Websocket/WebsocketTest.php @@ -7,7 +7,6 @@ use InvalidArgumentException; use Illuminate\Pipeline\Pipeline; use SwooleTW\Http\Tests\TestCase; -use Illuminate\Support\Facades\Auth; use SwooleTW\Http\Websocket\Websocket; use SwooleTW\Http\Websocket\Rooms\RoomContract; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; @@ -206,24 +205,6 @@ public function testGetUserId() $this->assertEquals($sender, $websocket->getUserId()); } - public function testGetUser() - { - $room = m::mock(RoomContract::class); - $room->shouldReceive('getRooms') - ->with($sender = 1) - ->once() - ->andReturn(['uid_1']); - - $user = m::mock(AuthenticatableContract::class); - Auth::shouldReceive('loginUsingId') - ->with($sender) - ->once() - ->andReturn($user); - - $websocket = $this->getWebsocket($room)->setSender($sender); - $this->assertEquals($user, $websocket->getUser()); - } - public function testReset() { $websocket = $this->getWebsocket();