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

Commit 0f716e2

Browse files
authored
Merge pull request #243 from swooletw/develop
Develop
2 parents 43c9266 + 4d28803 commit 0f716e2

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/Concerns/InteractsWithWebsocket.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use SwooleTW\Http\Server\Facades\Server;
1313
use SwooleTW\Http\Websocket\HandlerContract;
1414
use Illuminate\Contracts\Container\Container;
15+
use Swoole\WebSocket\Server as WebsocketServer;
1516
use SwooleTW\Http\Websocket\Rooms\RoomContract;
1617
use SwooleTW\Http\Exceptions\WebsocketNotSetInConfigException;
1718

@@ -129,7 +130,7 @@ public function onMessage($server, $frame)
129130
*/
130131
public function onClose($server, $fd, $reactorId)
131132
{
132-
if (! $this->isServerWebsocket($fd) || ! $server instanceof Websocket) {
133+
if (! $this->isServerWebsocket($fd) || ! $server instanceof WebsocketServer) {
133134
return;
134135
}
135136

@@ -229,7 +230,7 @@ protected function prepareWebsocket()
229230
*/
230231
protected function isServerWebsocket(int $fd): bool
231232
{
232-
return $this->container->make(Server::class)
233+
return (bool) $this->container->make(Server::class)
233234
->connection_info($fd)['websocket_status'] ?? false;
234235
}
235236

src/Websocket/Pusher.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public function shouldBroadcast(): bool
221221
*/
222222
public function isServerWebsocket(int $fd): bool
223223
{
224-
return $this->server->connection_info($fd)['websocket_status'] ?? false;
224+
return (bool) $this->server->connection_info($fd)['websocket_status'] ?? false;
225225
}
226226

227227
/**
@@ -245,8 +245,11 @@ protected function getWebsocketConnections(): array
245245
*/
246246
public function shouldPushToDescriptor(int $fd): bool
247247
{
248-
return $this->server->exist($fd)
249-
&& ($this->broadcast && $this->sender !== (int) $fd);
248+
if (! $this->server->exist($fd)) {
249+
return false;
250+
}
251+
252+
return $this->broadcast ? $this->sender !== (int) $fd : true;
250253
}
251254

252255
/**

tests/Websocket/PusherTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function testShouldPushToDescriptor()
115115
$server = m::mock(Server::class);
116116
$server->shouldReceive('exist')
117117
->with($fd = 1)
118-
->twice()
118+
->times(3)
119119
->andReturn(true);
120120

121121
$pusher = Pusher::make([
@@ -141,5 +141,17 @@ public function testShouldPushToDescriptor()
141141
], $server);
142142

143143
$this->assertFalse($pusher->shouldPushToDescriptor($fd));
144+
145+
$pusher = Pusher::make([
146+
'opcode' => 1,
147+
'sender' => 1,
148+
'fds' => [],
149+
'broadcast' => false,
150+
'assigned' => false,
151+
'event' => 'event',
152+
'message' => 'message'
153+
], $server);
154+
155+
$this->assertTrue($pusher->shouldPushToDescriptor($fd));
144156
}
145157
}

0 commit comments

Comments
 (0)