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

Commit c78033f

Browse files
authored
Merge pull request #8 from swooletw/develop
Develop
2 parents d9d9747 + 3a312b7 commit c78033f

File tree

6 files changed

+29
-59
lines changed

6 files changed

+29
-59
lines changed

src/Server/Manager.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,15 @@ public function onTask(HttpServer $server, $taskId, $fromId, $data)
256256
{
257257
$this->container['events']->fire('swoole.task', func_get_args());
258258

259-
// push websocket message
260-
if ($this->isWebsocket
261-
&& array_key_exists('action', $data)
262-
&& $data['action'] === Websocket::PUSH_ACTION) {
263-
$this->pushMessage($server, $data['data'] ?? []);
259+
try {
260+
// push websocket message
261+
if ($this->isWebsocket
262+
&& array_key_exists('action', $data)
263+
&& $data['action'] === Websocket::PUSH_ACTION) {
264+
$this->pushMessage($server, $data['data'] ?? []);
265+
}
266+
} catch (Exception $e) {
267+
$this->logServerError($e);
264268
}
265269
}
266270

src/Websocket/CanWebsocket.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function pushMessage(Server $server, array $data)
119119
$message = $this->formatter->output($event, $data['message']);
120120

121121
// attach sender if not broadcast
122-
if (! $broadcast && ! in_array($sender, $fds)) {
122+
if (! $broadcast && $sender && ! in_array($sender, $fds)) {
123123
$fds[] = $sender;
124124
}
125125

@@ -132,8 +132,9 @@ public function pushMessage(Server $server, array $data)
132132
}
133133
}
134134

135+
// push message to designated fds
135136
foreach ($fds as $fd) {
136-
if ($broadcast && $sender === (integer) $fd) {
137+
if (($broadcast && $sender === (integer) $fd) || ! $server->exist($fd)) {
137138
continue;
138139
}
139140
$server->push($fd, $message, $opcode);

src/Websocket/Rooms/TableRoom.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public function addAll(int $fd, array $roomNames)
3434
$rooms = $this->getRooms($fd);
3535

3636
foreach ($roomNames as $room) {
37-
$room = $this->encode($room);
38-
$sids = $this->getClients($room, false);
37+
$sids = $this->getClients($room);
3938

4039
if (in_array($fd, $sids)) {
4140
continue;
@@ -58,11 +57,11 @@ public function delete(int $fd, string $room)
5857
public function deleteAll(int $fd, array $roomNames = [])
5958
{
6059
$allRooms = $this->getRooms($fd);
61-
$rooms = count($roomNames) ? $this->encode($roomNames) : $allRooms;
60+
$rooms = count($roomNames) ? $roomNames : $allRooms;
6261

6362
$removeRooms = [];
6463
foreach ($rooms as $room) {
65-
$sids = $this->getClients($room, false);
64+
$sids = $this->getClients($room);
6665

6766
if (! in_array($fd, $sids)) {
6867
continue;
@@ -75,12 +74,8 @@ public function deleteAll(int $fd, array $roomNames = [])
7574
$this->setRooms($fd, array_values(array_diff($allRooms, $removeRooms)), 'sids');
7675
}
7776

78-
public function getClients(string $room, $hash = true)
77+
public function getClients(string $room)
7978
{
80-
if ($hash) {
81-
$room = $this->encode($room);
82-
}
83-
8479
return $this->getValue($room, 'rooms');
8580
}
8681

@@ -113,17 +108,6 @@ protected function initSidsTable()
113108
$this->sids->create();
114109
}
115110

116-
protected function encode($keys)
117-
{
118-
if (is_array($keys)) {
119-
return array_map(function ($key) {
120-
return md5($key);
121-
}, $keys);
122-
}
123-
124-
return md5($keys);
125-
}
126-
127111
public function setValue($key, array $value, string $table)
128112
{
129113
$this->checkTable($table);

src/Websocket/Websocket.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,6 @@ public function leaveAll(array $rooms = [])
9999
return $this;
100100
}
101101

102-
public function on(string $event, callable $callback)
103-
{
104-
//
105-
}
106-
107102
public function emit(string $event, $data)
108103
{
109104
app('swoole.server')->task([
@@ -117,7 +112,7 @@ public function emit(string $event, $data)
117112
]
118113
]);
119114

120-
$this->cleanData();
115+
$this->reset();
121116
}
122117

123118
public function in(string $room)
@@ -163,7 +158,7 @@ protected function getFds()
163158
return array_values($fds);
164159
}
165160

166-
protected function cleanData()
161+
protected function reset()
167162
{
168163
$this->isBroadcast = false;
169164
$this->sender = null;

tests/Websocket/TableRoomTest.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ public function testAddAll()
5656
{
5757
$this->tableRoom->addAll($key = 1, $values = ['foo', 'bar']);
5858

59-
$this->assertSame($this->encode($values), $this->tableRoom->getValue($key, $table = 'sids'));
60-
$this->assertSame([$key], $this->tableRoom->getValue($this->encode('foo'), 'rooms'));
61-
$this->assertSame([$key], $this->tableRoom->getValue($this->encode('bar'), 'rooms'));
59+
$this->assertSame($values, $this->tableRoom->getValue($key, $table = 'sids'));
60+
$this->assertSame([$key], $this->tableRoom->getValue('foo', 'rooms'));
61+
$this->assertSame([$key], $this->tableRoom->getValue('bar', 'rooms'));
6262
}
6363

6464
public function testAdd()
6565
{
6666
$this->tableRoom->add($key = 1, $value = 'foo');
6767

68-
$this->assertSame([$this->encode($value)], $this->tableRoom->getValue($key, $table = 'sids'));
69-
$this->assertSame([$key], $this->tableRoom->getValue($this->encode($value), 'rooms'));
68+
$this->assertSame([$value], $this->tableRoom->getValue($key, $table = 'sids'));
69+
$this->assertSame([$key], $this->tableRoom->getValue($value, 'rooms'));
7070
}
7171

7272
public function testDeleteAll()
@@ -75,18 +75,18 @@ public function testDeleteAll()
7575
$this->tableRoom->deleteAll($key);
7676

7777
$this->assertSame([], $this->tableRoom->getValue($key, $table = 'sids'));
78-
$this->assertSame([], $this->tableRoom->getValue($this->encode('foo'), 'rooms'));
79-
$this->assertSame([], $this->tableRoom->getValue($this->encode('bar'), 'rooms'));
78+
$this->assertSame([], $this->tableRoom->getValue('foo', 'rooms'));
79+
$this->assertSame([], $this->tableRoom->getValue('bar', 'rooms'));
8080
}
8181

8282
public function testDelete()
8383
{
8484
$this->tableRoom->addAll($key = 1, $values = ['foo', 'bar']);
8585
$this->tableRoom->delete($key, 'foo');
8686

87-
$this->assertSame([$this->encode('bar')], $this->tableRoom->getValue($key, $table = 'sids'));
88-
$this->assertSame([], $this->tableRoom->getValue($this->encode('foo'), 'rooms'));
89-
$this->assertSame([$key], $this->tableRoom->getValue($this->encode('bar'), 'rooms'));
87+
$this->assertSame(['bar'], $this->tableRoom->getValue($key, $table = 'sids'));
88+
$this->assertSame([], $this->tableRoom->getValue('foo', 'rooms'));
89+
$this->assertSame([$key], $this->tableRoom->getValue('bar', 'rooms'));
9090
}
9191

9292
public function testGetRooms()
@@ -106,17 +106,8 @@ public function testGetClients()
106106
$this->tableRoom->add($keys[1], $room);
107107

108108
$this->assertSame(
109-
$this->tableRoom->getValue($this->encode($room), $table = 'rooms'),
109+
$this->tableRoom->getValue($room, $table = 'rooms'),
110110
$this->tableRoom->getClients($room)
111111
);
112112
}
113-
114-
protected function encode($keys)
115-
{
116-
$reflection = new \ReflectionClass($this->tableRoom);
117-
$method = $reflection->getMethod('encode');
118-
$method->setAccessible(true);
119-
120-
return $method->invokeArgs($this->tableRoom, [$keys]);
121-
}
122113
}

tests/Websocket/WebsocketTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99

1010
class WebsocketTest extends TestCase
1111
{
12-
public function setUp()
13-
{
14-
//
15-
}
16-
1712
public function testSetBroadcast()
1813
{
1914
$websocket = $this->getWebsocket();

0 commit comments

Comments
 (0)