Skip to content

Commit

Permalink
Server setContext
Browse files Browse the repository at this point in the history
  • Loading branch information
sirn-se committed Sep 14, 2024
1 parent d8a4c6e commit 4034a88
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
"require": {
"php": "^8.1",
"phrity/net-uri": "^2.1",
"phrity/net-stream": "^2.0",
"phrity/net-stream": "^2.1",
"psr/http-message": "^1.1 | ^2.0",
"psr/log": "^1.0 | ^2.0 | ^3.0"
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.0",
"phpunit/phpunit": "^10.0 | ^11.0",
"phrity/net-mock": "^2.0",
"phrity/net-mock": "^2.1",
"phrity/util-errorhandler": "^1.1",
"squizlabs/php_codesniffer": "^3.5"
}
Expand Down
13 changes: 13 additions & 0 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Server implements LoggerAwareInterface, Stringable
private LoggerInterface $logger;
private int $timeout = 60;
private int $frameSize = 4096;
private array $context = [];

// Internal resources
private StreamFactory $streamFactory;
Expand Down Expand Up @@ -244,6 +245,17 @@ public function getWritableConnections(): array
});
}

/**
* Set connection context.
* @param array $context Context as array, see https://www.php.net/manual/en/context.php
* @return self
*/
public function setContext(array $context): self
{
$this->context = $context;
return $this;
}

/**
* Add a middleware.
* @param WebSocket\Middleware\MiddlewareInterface $middleware
Expand Down Expand Up @@ -455,6 +467,7 @@ protected function createSocketServer(): void
try {
$uri = new Uri("{$this->scheme}://0.0.0.0:{$this->port}");
$this->server = $this->streamFactory->createSocketServer($uri);
$this->server->setContext($this->context);
$this->streams = $this->streamFactory->createStreamCollection();
$this->streams->attach($this->server, '@server');
$this->logger->info("[server] Starting server on {$uri}.");
Expand Down
5 changes: 4 additions & 1 deletion tests/mock/MockStreamTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function (string $method, array $params) use ($host, $path, $headers): void {

/* ---------- WebSocket Server combinded asserts --------------------------------------------------------------- */

private function expectWsServerSetup(string $scheme = 'tcp', int $port = 8000): void
private function expectWsServerSetup(string $scheme = 'tcp', int $port = 8000, array $context = []): void
{
$this->expectStreamFactoryCreateSocketServer()->addAssert(function ($method, $params) use ($scheme, $port) {
$this->assertInstanceOf('Phrity\Net\Uri', $params[0]);
Expand All @@ -116,6 +116,9 @@ private function expectWsServerSetup(string $scheme = 'tcp', int $port = 8000):
});
$this->expectSocketServerGetTransports();
$this->expectSocketServerGetMetadata();
$this->expectSocketServerSetContext()->addAssert(function ($method, $params) use ($context) {
$this->assertEquals($context, $params[0]);
});
$this->expectStreamFactoryCreateStreamCollection();
$this->expectStreamCollection();
$this->expectStreamCollectionAttach()->addAssert(function ($method, $params) {
Expand Down
23 changes: 23 additions & 0 deletions tests/suites/server/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,27 @@ public function testServerConfiguration(): void
$this->expectSocketStreamClose();
unset($server);
}

public function testContextOption(): void
{
$this->expectStreamFactory();
$server = new Server(8000);
$this->assertSame($server, $server->setStreamFactory(new StreamFactory()));
$this->assertSame($server, $server->setContext(['ssl' => ['verify_peer' => false]]));

$this->expectWsServerSetup(scheme: 'tcp', port: 8000, context: ['ssl' => ['verify_peer' => false]]);
$this->expectStreamCollectionWaitRead()->addAssert(function ($method, $params) {
$this->assertEquals(60, $params[0]);
});
$this->expectStreamCollection()->addAssert(function ($method, $params) use ($server) {
$this->assertTrue($server->isRunning());
$this->assertEquals(0, $server->getConnectionCount());
$server->stop();
});
$server->start();
$this->assertEquals('WebSocket\Server(tcp://0.0.0.0:8000)', "{$server}");
$this->assertFalse($server->isRunning());

unset($server);
}
}

0 comments on commit 4034a88

Please sign in to comment.