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 #8 from swooletw/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
albertcht authored Apr 15, 2018
2 parents d9d9747 + 3a312b7 commit c78033f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 59 deletions.
14 changes: 9 additions & 5 deletions src/Server/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/Websocket/CanWebsocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
Expand Down
24 changes: 4 additions & 20 deletions src/Websocket/Rooms/TableRoom.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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');
}

Expand Down Expand Up @@ -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);
Expand Down
9 changes: 2 additions & 7 deletions src/Websocket/Websocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -117,7 +112,7 @@ public function emit(string $event, $data)
]
]);

$this->cleanData();
$this->reset();
}

public function in(string $room)
Expand Down Expand Up @@ -163,7 +158,7 @@ protected function getFds()
return array_values($fds);
}

protected function cleanData()
protected function reset()
{
$this->isBroadcast = false;
$this->sender = null;
Expand Down
31 changes: 11 additions & 20 deletions tests/Websocket/TableRoomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -75,18 +75,18 @@ 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()
{
$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()
Expand All @@ -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]);
}
}
5 changes: 0 additions & 5 deletions tests/Websocket/WebsocketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@

class WebsocketTest extends TestCase
{
public function setUp()
{
//
}

public function testSetBroadcast()
{
$websocket = $this->getWebsocket();
Expand Down

0 comments on commit c78033f

Please sign in to comment.