From 1033684b6779f66c1855786a388cfe9ad41210cc Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Fri, 1 Feb 2019 22:37:05 +0800 Subject: [PATCH] fix table prepare for websocket room --- src/Concerns/InteractsWithWebsocket.php | 6 +++++- src/Websocket/Rooms/RedisRoom.php | 8 ++++---- src/Websocket/Rooms/TableRoom.php | 12 ++++++------ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Concerns/InteractsWithWebsocket.php b/src/Concerns/InteractsWithWebsocket.php index 8c17bc99..96457c60 100644 --- a/src/Concerns/InteractsWithWebsocket.php +++ b/src/Concerns/InteractsWithWebsocket.php @@ -327,7 +327,11 @@ protected function bindRoom(): void $settings = $config->get("swoole_websocket.settings.{$driver}"); $className = $config->get("swoole_websocket.drivers.{$driver}"); - return $this->createRoom($className, $settings); + // create room instance and initialize + $room = $this->createRoom($className, $settings); + $room->prepare(); + + return $room; }); $this->app->alias(RoomContract::class, 'swoole.room'); diff --git a/src/Websocket/Rooms/RedisRoom.php b/src/Websocket/Rooms/RedisRoom.php index d0d39fd4..2a5ba9e9 100644 --- a/src/Websocket/Rooms/RedisRoom.php +++ b/src/Websocket/Rooms/RedisRoom.php @@ -98,7 +98,7 @@ public function getRedis() */ public function add(int $fd, $rooms) { - $rooms = \is_array($rooms) ? $rooms : [$rooms]; + $rooms = is_array($rooms) ? $rooms : [$rooms]; $this->addValue($fd, $rooms, RoomContract::DESCRIPTORS_KEY); @@ -115,8 +115,8 @@ public function add(int $fd, $rooms) */ public function delete(int $fd, $rooms) { - $rooms = \is_array($rooms) ? $rooms : [$rooms]; - $rooms = \count($rooms) ? $rooms : $this->getRooms($fd); + $rooms = is_array($rooms) ? $rooms : [$rooms]; + $rooms = count($rooms) ? $rooms : $this->getRooms($fd); $this->removeValue($fd, $rooms, RoomContract::DESCRIPTORS_KEY); @@ -203,7 +203,7 @@ public function getRooms(int $fd) protected function checkTable(string $table) { if (! in_array($table, [RoomContract::ROOMS_KEY, RoomContract::DESCRIPTORS_KEY])) { - throw new \InvalidArgumentException('Invalid table name.'); + throw new \InvalidArgumentException("Invalid table name: `{$table}`."); } } diff --git a/src/Websocket/Rooms/TableRoom.php b/src/Websocket/Rooms/TableRoom.php index 4d97d78e..8b5e6fc7 100644 --- a/src/Websocket/Rooms/TableRoom.php +++ b/src/Websocket/Rooms/TableRoom.php @@ -53,7 +53,7 @@ public function prepare(): RoomContract public function add(int $fd, $roomNames) { $rooms = $this->getRooms($fd); - $roomNames = \is_array($roomNames) ? $roomNames : [$roomNames]; + $roomNames = is_array($roomNames) ? $roomNames : [$roomNames]; foreach ($roomNames as $room) { $fds = $this->getClients($room); @@ -80,8 +80,8 @@ public function add(int $fd, $roomNames) public function delete(int $fd, $roomNames = []) { $allRooms = $this->getRooms($fd); - $roomNames = \is_array($roomNames) ? $roomNames : [$roomNames]; - $rooms = \count($roomNames) ? $roomNames : $allRooms; + $roomNames = is_array($roomNames) ? $roomNames : [$roomNames]; + $rooms = count($roomNames) ? $roomNames : $allRooms; $removeRooms = []; foreach ($rooms as $room) { @@ -177,7 +177,7 @@ public function setValue($key, array $value, string $table) { $this->checkTable($table); - $this->$table->set($key, ['value' => \json_encode($value)]); + $this->$table->set($key, ['value' => json_encode($value)]); return $this; } @@ -196,7 +196,7 @@ public function getValue(string $key, string $table) $value = $this->$table->get($key); - return $value ? \json_decode($value['value'], true) : []; + return $value ? json_decode($value['value'], true) : []; } /** @@ -207,7 +207,7 @@ public function getValue(string $key, string $table) protected function checkTable(string $table) { if (! property_exists($this, $table) || ! $this->$table instanceof Table) { - throw new \InvalidArgumentException('Invalid table name.'); + throw new \InvalidArgumentException("Invalid table name: `{$table}`."); } } }