Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #204 from swooletw/develop
Browse files Browse the repository at this point in the history
fix table prepare for websocket room
  • Loading branch information
albertcht authored Feb 1, 2019
2 parents 74f2673 + 1033684 commit 761870e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
6 changes: 5 additions & 1 deletion src/Concerns/InteractsWithWebsocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
8 changes: 4 additions & 4 deletions src/Websocket/Rooms/RedisRoom.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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}`.");
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/Websocket/Rooms/TableRoom.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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) : [];
}

/**
Expand All @@ -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}`.");
}
}
}

0 comments on commit 761870e

Please sign in to comment.