diff --git a/src/Server/Manager.php b/src/Server/Manager.php index 567d3b01..89ef05be 100644 --- a/src/Server/Manager.php +++ b/src/Server/Manager.php @@ -256,11 +256,15 @@ public function onTask(HttpServer $server, $taskId, $fromId, $data) { $this->container['events']->fire('swoole.task', func_get_args()); - // push websocket message - if ($this->isWebsocket - && array_key_exists('action', $data) - && $data['action'] === Websocket::PUSH_ACTION) { - $this->pushMessage($server, $data['data'] ?? []); + try { + // push websocket message + if ($this->isWebsocket + && array_key_exists('action', $data) + && $data['action'] === Websocket::PUSH_ACTION) { + $this->pushMessage($server, $data['data'] ?? []); + } + } catch (Exception $e) { + $this->logServerError($e); } } diff --git a/src/Websocket/CanWebsocket.php b/src/Websocket/CanWebsocket.php index f68caa3d..a6308cac 100644 --- a/src/Websocket/CanWebsocket.php +++ b/src/Websocket/CanWebsocket.php @@ -119,7 +119,7 @@ public function pushMessage(Server $server, array $data) $message = $this->formatter->output($event, $data['message']); // attach sender if not broadcast - if (! $broadcast && ! in_array($sender, $fds)) { + if (! $broadcast && $sender && ! in_array($sender, $fds)) { $fds[] = $sender; } @@ -132,8 +132,9 @@ public function pushMessage(Server $server, array $data) } } + // push message to designated fds foreach ($fds as $fd) { - if ($broadcast && $sender === (integer) $fd) { + if (($broadcast && $sender === (integer) $fd) || ! $server->exist($fd)) { continue; } $server->push($fd, $message, $opcode); diff --git a/src/Websocket/Rooms/TableRoom.php b/src/Websocket/Rooms/TableRoom.php index e93ccacf..5327ab89 100644 --- a/src/Websocket/Rooms/TableRoom.php +++ b/src/Websocket/Rooms/TableRoom.php @@ -34,8 +34,7 @@ public function addAll(int $fd, array $roomNames) $rooms = $this->getRooms($fd); foreach ($roomNames as $room) { - $room = $this->encode($room); - $sids = $this->getClients($room, false); + $sids = $this->getClients($room); if (in_array($fd, $sids)) { continue; @@ -58,11 +57,11 @@ public function delete(int $fd, string $room) public function deleteAll(int $fd, array $roomNames = []) { $allRooms = $this->getRooms($fd); - $rooms = count($roomNames) ? $this->encode($roomNames) : $allRooms; + $rooms = count($roomNames) ? $roomNames : $allRooms; $removeRooms = []; foreach ($rooms as $room) { - $sids = $this->getClients($room, false); + $sids = $this->getClients($room); if (! in_array($fd, $sids)) { continue; @@ -75,12 +74,8 @@ public function deleteAll(int $fd, array $roomNames = []) $this->setRooms($fd, array_values(array_diff($allRooms, $removeRooms)), 'sids'); } - public function getClients(string $room, $hash = true) + public function getClients(string $room) { - if ($hash) { - $room = $this->encode($room); - } - return $this->getValue($room, 'rooms'); } @@ -113,17 +108,6 @@ protected function initSidsTable() $this->sids->create(); } - protected function encode($keys) - { - if (is_array($keys)) { - return array_map(function ($key) { - return md5($key); - }, $keys); - } - - return md5($keys); - } - public function setValue($key, array $value, string $table) { $this->checkTable($table); diff --git a/src/Websocket/Websocket.php b/src/Websocket/Websocket.php index ffedd86a..f740ffed 100644 --- a/src/Websocket/Websocket.php +++ b/src/Websocket/Websocket.php @@ -99,11 +99,6 @@ public function leaveAll(array $rooms = []) return $this; } - public function on(string $event, callable $callback) - { - // - } - public function emit(string $event, $data) { app('swoole.server')->task([ @@ -117,7 +112,7 @@ public function emit(string $event, $data) ] ]); - $this->cleanData(); + $this->reset(); } public function in(string $room) @@ -163,7 +158,7 @@ protected function getFds() return array_values($fds); } - protected function cleanData() + protected function reset() { $this->isBroadcast = false; $this->sender = null; diff --git a/tests/Websocket/TableRoomTest.php b/tests/Websocket/TableRoomTest.php index 62b257ef..5f99a3d1 100644 --- a/tests/Websocket/TableRoomTest.php +++ b/tests/Websocket/TableRoomTest.php @@ -56,17 +56,17 @@ public function testAddAll() { $this->tableRoom->addAll($key = 1, $values = ['foo', 'bar']); - $this->assertSame($this->encode($values), $this->tableRoom->getValue($key, $table = 'sids')); - $this->assertSame([$key], $this->tableRoom->getValue($this->encode('foo'), 'rooms')); - $this->assertSame([$key], $this->tableRoom->getValue($this->encode('bar'), 'rooms')); + $this->assertSame($values, $this->tableRoom->getValue($key, $table = 'sids')); + $this->assertSame([$key], $this->tableRoom->getValue('foo', 'rooms')); + $this->assertSame([$key], $this->tableRoom->getValue('bar', 'rooms')); } public function testAdd() { $this->tableRoom->add($key = 1, $value = 'foo'); - $this->assertSame([$this->encode($value)], $this->tableRoom->getValue($key, $table = 'sids')); - $this->assertSame([$key], $this->tableRoom->getValue($this->encode($value), 'rooms')); + $this->assertSame([$value], $this->tableRoom->getValue($key, $table = 'sids')); + $this->assertSame([$key], $this->tableRoom->getValue($value, 'rooms')); } public function testDeleteAll() @@ -75,8 +75,8 @@ public function testDeleteAll() $this->tableRoom->deleteAll($key); $this->assertSame([], $this->tableRoom->getValue($key, $table = 'sids')); - $this->assertSame([], $this->tableRoom->getValue($this->encode('foo'), 'rooms')); - $this->assertSame([], $this->tableRoom->getValue($this->encode('bar'), 'rooms')); + $this->assertSame([], $this->tableRoom->getValue('foo', 'rooms')); + $this->assertSame([], $this->tableRoom->getValue('bar', 'rooms')); } public function testDelete() @@ -84,9 +84,9 @@ public function testDelete() $this->tableRoom->addAll($key = 1, $values = ['foo', 'bar']); $this->tableRoom->delete($key, 'foo'); - $this->assertSame([$this->encode('bar')], $this->tableRoom->getValue($key, $table = 'sids')); - $this->assertSame([], $this->tableRoom->getValue($this->encode('foo'), 'rooms')); - $this->assertSame([$key], $this->tableRoom->getValue($this->encode('bar'), 'rooms')); + $this->assertSame(['bar'], $this->tableRoom->getValue($key, $table = 'sids')); + $this->assertSame([], $this->tableRoom->getValue('foo', 'rooms')); + $this->assertSame([$key], $this->tableRoom->getValue('bar', 'rooms')); } public function testGetRooms() @@ -106,17 +106,8 @@ public function testGetClients() $this->tableRoom->add($keys[1], $room); $this->assertSame( - $this->tableRoom->getValue($this->encode($room), $table = 'rooms'), + $this->tableRoom->getValue($room, $table = 'rooms'), $this->tableRoom->getClients($room) ); } - - protected function encode($keys) - { - $reflection = new \ReflectionClass($this->tableRoom); - $method = $reflection->getMethod('encode'); - $method->setAccessible(true); - - return $method->invokeArgs($this->tableRoom, [$keys]); - } } diff --git a/tests/Websocket/WebsocketTest.php b/tests/Websocket/WebsocketTest.php index 893ef193..d549ea95 100644 --- a/tests/Websocket/WebsocketTest.php +++ b/tests/Websocket/WebsocketTest.php @@ -9,11 +9,6 @@ class WebsocketTest extends TestCase { - public function setUp() - { - // - } - public function testSetBroadcast() { $websocket = $this->getWebsocket();