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 #243 from swooletw/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
albertcht authored Mar 9, 2019
2 parents 43c9266 + 4d28803 commit 0f716e2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/Concerns/InteractsWithWebsocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use SwooleTW\Http\Server\Facades\Server;
use SwooleTW\Http\Websocket\HandlerContract;
use Illuminate\Contracts\Container\Container;
use Swoole\WebSocket\Server as WebsocketServer;
use SwooleTW\Http\Websocket\Rooms\RoomContract;
use SwooleTW\Http\Exceptions\WebsocketNotSetInConfigException;

Expand Down Expand Up @@ -129,7 +130,7 @@ public function onMessage($server, $frame)
*/
public function onClose($server, $fd, $reactorId)
{
if (! $this->isServerWebsocket($fd) || ! $server instanceof Websocket) {
if (! $this->isServerWebsocket($fd) || ! $server instanceof WebsocketServer) {
return;
}

Expand Down Expand Up @@ -229,7 +230,7 @@ protected function prepareWebsocket()
*/
protected function isServerWebsocket(int $fd): bool
{
return $this->container->make(Server::class)
return (bool) $this->container->make(Server::class)
->connection_info($fd)['websocket_status'] ?? false;
}

Expand Down
9 changes: 6 additions & 3 deletions src/Websocket/Pusher.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function shouldBroadcast(): bool
*/
public function isServerWebsocket(int $fd): bool
{
return $this->server->connection_info($fd)['websocket_status'] ?? false;
return (bool) $this->server->connection_info($fd)['websocket_status'] ?? false;
}

/**
Expand All @@ -245,8 +245,11 @@ protected function getWebsocketConnections(): array
*/
public function shouldPushToDescriptor(int $fd): bool
{
return $this->server->exist($fd)
&& ($this->broadcast && $this->sender !== (int) $fd);
if (! $this->server->exist($fd)) {
return false;
}

return $this->broadcast ? $this->sender !== (int) $fd : true;
}

/**
Expand Down
14 changes: 13 additions & 1 deletion tests/Websocket/PusherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function testShouldPushToDescriptor()
$server = m::mock(Server::class);
$server->shouldReceive('exist')
->with($fd = 1)
->twice()
->times(3)
->andReturn(true);

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

$this->assertFalse($pusher->shouldPushToDescriptor($fd));

$pusher = Pusher::make([
'opcode' => 1,
'sender' => 1,
'fds' => [],
'broadcast' => false,
'assigned' => false,
'event' => 'event',
'message' => 'message'
], $server);

$this->assertTrue($pusher->shouldPushToDescriptor($fd));
}
}

0 comments on commit 0f716e2

Please sign in to comment.