Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.

Commit 761870e

Browse files
authored
Merge pull request #204 from swooletw/develop
fix table prepare for websocket room
2 parents 74f2673 + 1033684 commit 761870e

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

src/Concerns/InteractsWithWebsocket.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,11 @@ protected function bindRoom(): void
327327
$settings = $config->get("swoole_websocket.settings.{$driver}");
328328
$className = $config->get("swoole_websocket.drivers.{$driver}");
329329

330-
return $this->createRoom($className, $settings);
330+
// create room instance and initialize
331+
$room = $this->createRoom($className, $settings);
332+
$room->prepare();
333+
334+
return $room;
331335
});
332336

333337
$this->app->alias(RoomContract::class, 'swoole.room');

src/Websocket/Rooms/RedisRoom.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function getRedis()
9898
*/
9999
public function add(int $fd, $rooms)
100100
{
101-
$rooms = \is_array($rooms) ? $rooms : [$rooms];
101+
$rooms = is_array($rooms) ? $rooms : [$rooms];
102102

103103
$this->addValue($fd, $rooms, RoomContract::DESCRIPTORS_KEY);
104104

@@ -115,8 +115,8 @@ public function add(int $fd, $rooms)
115115
*/
116116
public function delete(int $fd, $rooms)
117117
{
118-
$rooms = \is_array($rooms) ? $rooms : [$rooms];
119-
$rooms = \count($rooms) ? $rooms : $this->getRooms($fd);
118+
$rooms = is_array($rooms) ? $rooms : [$rooms];
119+
$rooms = count($rooms) ? $rooms : $this->getRooms($fd);
120120

121121
$this->removeValue($fd, $rooms, RoomContract::DESCRIPTORS_KEY);
122122

@@ -203,7 +203,7 @@ public function getRooms(int $fd)
203203
protected function checkTable(string $table)
204204
{
205205
if (! in_array($table, [RoomContract::ROOMS_KEY, RoomContract::DESCRIPTORS_KEY])) {
206-
throw new \InvalidArgumentException('Invalid table name.');
206+
throw new \InvalidArgumentException("Invalid table name: `{$table}`.");
207207
}
208208
}
209209

src/Websocket/Rooms/TableRoom.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function prepare(): RoomContract
5353
public function add(int $fd, $roomNames)
5454
{
5555
$rooms = $this->getRooms($fd);
56-
$roomNames = \is_array($roomNames) ? $roomNames : [$roomNames];
56+
$roomNames = is_array($roomNames) ? $roomNames : [$roomNames];
5757

5858
foreach ($roomNames as $room) {
5959
$fds = $this->getClients($room);
@@ -80,8 +80,8 @@ public function add(int $fd, $roomNames)
8080
public function delete(int $fd, $roomNames = [])
8181
{
8282
$allRooms = $this->getRooms($fd);
83-
$roomNames = \is_array($roomNames) ? $roomNames : [$roomNames];
84-
$rooms = \count($roomNames) ? $roomNames : $allRooms;
83+
$roomNames = is_array($roomNames) ? $roomNames : [$roomNames];
84+
$rooms = count($roomNames) ? $roomNames : $allRooms;
8585

8686
$removeRooms = [];
8787
foreach ($rooms as $room) {
@@ -177,7 +177,7 @@ public function setValue($key, array $value, string $table)
177177
{
178178
$this->checkTable($table);
179179

180-
$this->$table->set($key, ['value' => \json_encode($value)]);
180+
$this->$table->set($key, ['value' => json_encode($value)]);
181181

182182
return $this;
183183
}
@@ -196,7 +196,7 @@ public function getValue(string $key, string $table)
196196

197197
$value = $this->$table->get($key);
198198

199-
return $value ? \json_decode($value['value'], true) : [];
199+
return $value ? json_decode($value['value'], true) : [];
200200
}
201201

202202
/**
@@ -207,7 +207,7 @@ public function getValue(string $key, string $table)
207207
protected function checkTable(string $table)
208208
{
209209
if (! property_exists($this, $table) || ! $this->$table instanceof Table) {
210-
throw new \InvalidArgumentException('Invalid table name.');
210+
throw new \InvalidArgumentException("Invalid table name: `{$table}`.");
211211
}
212212
}
213213
}

0 commit comments

Comments
 (0)