Skip to content

Commit

Permalink
Server get*Connection methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sirn-se committed Jun 30, 2024
1 parent d114efb commit f32897a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 8 deletions.
4 changes: 4 additions & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

> PHP version `^8.1`
### `3.1.0`

* Server `getConnections()`, `getReadableConnections()`, `getWritableConnections()` (@sirn-se)

### `3.0.0`

* Support `psr/log v3` (@sirn-se)
Expand Down
20 changes: 18 additions & 2 deletions docs/Server.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ echo "scheme: {$server->getScheme()}\n";
echo "timeout: {$server->getTimeout()}s\n";
echo "frame size: {$server->getFrameSize()}b\n";
echo "running: {$server->isRunning()}\n";
echo "connections: {$server->getConnectionCount()}\n";
echo "ssl: {$server->isSsl()}\n";
```

Expand Down Expand Up @@ -170,12 +169,29 @@ $server->start();
// Stop server - When called, server will no longer listen to incoming messages but will not disconnect clients
$server->stop();

//Disconnect server - Server will immediately stop and disconnect all clients without normal close procedure
// Disconnect server - Server will immediately stop and disconnect all clients without normal close procedure
$server->disconnect();
```

To shut down server in an orderly fashion, you should first close all connected clients.


## Server operations

```php
// Number of connected clients
$server->getConnectionCount();

// Get all current connections (may be in any state)
$server->getConnections();

// Get all readable connections
$server->getReadableConnections();

// Get all writable connections
$server->getWritableConnections();
```

## Connection control

The Connection instance have some additional functions, besides sending messages to client.
Expand Down
33 changes: 32 additions & 1 deletion src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,37 @@ public function getConnectionCount(): int
return count($this->connections);
}

/**
* Get currently connected clients.
* @return array Connections
*/
public function getConnections(): array
{
return $this->connections;
}

/**
* Get currently readable clients.
* @return array Connections
*/
public function getReadableConnections(): array
{
return array_filter($this->connections, function (Connection $connection) {
return $connection->isReadable();
});
}

/**
* Get currently writable clients.
* @return array Connections
*/
public function getWritableConnections(): array
{
return array_filter($this->connections, function (Connection $connection) {
return $connection->isWritable();
});
}

/**
* Add a middleware.
* @param WebSocket\Middleware\MiddlewareInterface $middleware
Expand Down Expand Up @@ -304,7 +335,7 @@ public function start(): void
if ($connection) {
$connection->close($e->getCloseStatus(), $e->getMessage());
}
$this->logger->error("[server] sss {$e->getMessage()}");
$this->logger->error("[server] {$e->getMessage()}");
$this->dispatch('error', [$this, $connection, $e]);
}
}
Expand Down
11 changes: 6 additions & 5 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ make test

## Continuous integration

GitHub Actions are run on PHP versions `7.4`, `8.0`, `8.1` and `8.2`.
GitHub Actions are run on PHP versions `8.1`, `8.2`, `8.3` and `8.4`.

Code coverage by [Coveralls](https://coveralls.io/github/Textalk/websocket-php).
Code coverage by [Coveralls](https://coveralls.io/github/sirn-se/websocket-php).


## Test strategy

Test set up overloads various stream and socket functions,
and use "scripts" to define and mock input/output of these functions.
Uses the [phrity/net-mock](https://packagist.org/packages/phrity/net-mock) library to mock
stream operations, implementing `expect<method>` before methods that uses stream interactions
are called.

This set up negates the dependency on running servers,
This set up negates the dependency on running actual servers,
and allow testing various errors that might occur.
8 changes: 8 additions & 0 deletions tests/suites/server/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public function testServerDefaults(): void
$this->assertEquals('tcp', $server->getScheme());
$this->assertFalse($server->isRunning());
$this->assertEquals(0, $server->getConnectionCount());
$this->assertEmpty($server->getConnections());
$this->assertEmpty($server->getReadableConnections());
$this->assertEmpty($server->getWritableConnections());

$this->expectWsServerSetup(scheme: 'tcp', port: 8000);
$this->expectStreamCollectionWaitRead()->addAssert(function ($method, $params) {
Expand Down Expand Up @@ -119,6 +122,11 @@ public function testServerConfiguration(): void
$this->assertEquals('ssl', $server->getScheme());
$this->assertFalse($server->isRunning());
$this->assertEquals(1, $server->getConnectionCount());
$this->assertCount(1, $server->getConnections());
$this->expectSocketStreamIsReadable();
$this->assertCount(1, $server->getReadableConnections());
$this->expectSocketStreamIsWritable();
$this->assertCount(1, $server->getWritableConnections());

$this->expectSocketStreamIsConnected();
$this->expectSocketStreamClose();
Expand Down
13 changes: 13 additions & 0 deletions tests/suites/server/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,11 @@ public function testRunBadOpcodeException(): void

// Should not have closed
$this->assertEquals(1, $server->getConnectionCount());
$this->assertCount(1, $server->getConnections());
$this->expectSocketStreamIsReadable();
$this->assertCount(1, $server->getReadableConnections());
$this->expectSocketStreamIsWritable();
$this->assertCount(1, $server->getWritableConnections());

$this->expectSocketStreamClose();
$this->expectSocketServerClose();
Expand Down Expand Up @@ -510,6 +515,9 @@ public function testRunConnectionClosedException(): void

// Should be closed
$this->assertEquals(0, $server->getConnectionCount());
$this->assertEmpty($server->getConnections());
$this->assertEmpty($server->getReadableConnections());
$this->assertEmpty($server->getWritableConnections());

$this->expectSocketServerClose();
$server->disconnect();
Expand Down Expand Up @@ -551,6 +559,11 @@ public function testRunServerException(): void

// Should not have closed
$this->assertEquals(1, $server->getConnectionCount());
$this->assertCount(1, $server->getConnections());
$this->expectSocketStreamIsReadable();
$this->assertCount(1, $server->getReadableConnections());
$this->expectSocketStreamIsWritable();
$this->assertCount(1, $server->getWritableConnections());

$this->expectSocketStreamClose();
$this->expectSocketServerClose();
Expand Down

0 comments on commit f32897a

Please sign in to comment.