Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onHandshake method #65

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### `3.1.0`

* Server `getConnections()`, `getReadableConnections()`, `getWritableConnections()` (@sirn-se)
* `onHandshake(...)` listener (will deprecate `onConnect(...)`) (@sirn-se)

### `3.0.0`

Expand Down
2 changes: 1 addition & 1 deletion docs/Examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ $client
->addMiddleware(new PingResponder())
// Add ping interval middleware as heartbeat to keep connection open
->addMiddleware(new PingInterval(interval: 30))
->onConnect(function (Client $client, Connection $connection, ResponseInterface $response) {
->onHandshake(function (Client $client, Connection $connection, RequestInterface $request, ResponseInterface $response) {
// Initial message, typically some authorization or configuration
// This will be called everytime the client connect or reconnect
$client->text($initial_message);
Expand Down
8 changes: 4 additions & 4 deletions docs/Listener.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ $client_or_server

These listeners are called when the Client or Server connects and disconnects.

* On Client, the `onConnect()` will receive a `Response` as last argument
* On Server, the `onConnect()` will receive a `ServerRequest` as last argument
* On Client, the `onHandshake()` will receive `Request` and `Response` as last arguments
* On Server, the `onHandshake()` will receive `ServerRequest` and `Response` as last arguments

```php
$client_or_server
// Called when a connection is established
->onConnect(function (WebSocket\Client|WebSocket\Server $client_or_server WebSocket\Connection $connection, Psr\Http\Message\ServerRequestInterface|Psr\Http\Message\ResponseInterface $request_or_respone) {
// Called when a connection and handshake established
->onHandshake(function (WebSocket\Client|WebSocket\Server $client_or_server WebSocket\Connection $connection, Psr\Http\Message\RequestInterface|Psr\Http\Message\ServerRequestInterface $request, Psr\Http\Message\ResponseInterface $respone) {
// Act on connect
})
// Called when a connection is closed
Expand Down
10 changes: 6 additions & 4 deletions docs/Migrate_2_3.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
[Documentation](Index.md) / Migration v2 -> v3

# Websocket: Migration v2 -> v3

Version 3.x has few changes compared to previous version.

# Breaking changes
## Breaking changes

## setLogger
### setLogger

```php
Client->setLogger(LoggerInterface $logger): void
Expand All @@ -17,7 +19,7 @@ This means method return can not be chained.

The change makes v3 complient with `psr/log v3`.

## receive
### receive

```php
Client->receive(): Message
Expand All @@ -26,6 +28,6 @@ Client->receive(): Message
The method no longer has `Message|null` as return type.
It never returned `null` before, so only the method profile has changed.

# Extending
## Extending

When extending classes in this repo, you might need to implement typed properties in child class.
4 changes: 2 additions & 2 deletions examples/echoserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
}

echo "# Listening on port {$server->getPort()}\n";
$server->onConnect(function ($server, $connection, $handshake) {
echo "> [{$connection->getRemoteName()}] Client connected {$handshake->getUri()}\n";
$server->onHandshake(function ($server, $connection, $request, $response) {
echo "> [{$connection->getRemoteName()}] Client connected {$request->getUri()}\n";
})->onDisconnect(function ($server, $connection) {
echo "> [{$connection->getRemoteName()}] Client disconnected\n";
})->onText(function ($server, $connection, $message) {
Expand Down
4 changes: 2 additions & 2 deletions examples/random_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
}

echo "# Listening on {$options['uri']}\n";
$client->onConnect(function ($client, $connection, $handshake) {
echo "> [{$connection->getRemoteName()}] Server connected {$handshake->getStatusCode()}\n";
$client->onHandshake(function ($server, $connection, $request, $response) {
echo "> [{$connection->getRemoteName()}] Server connected {$response->getStatusCode()}\n";
})->onDisconnect(function ($client, $connection) {
echo "> [{$connection->getRemoteName()}] Server disconnected\n";
})->onText(function ($client, $connection, $message) {
Expand Down
4 changes: 2 additions & 2 deletions examples/random_server.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
}

echo "# Listening on port {$server->getPort()}\n";
$server->onConnect(function ($server, $connection, $handshake) {
echo "> [{$connection->getRemoteName()}] Client connected {$handshake->getUri()}\n";
$server->onHandshake(function ($server, $connection, $request, $response) {
echo "> [{$connection->getRemoteName()}] Client connected {$request->getUri()}\n";
})->onDisconnect(function ($server, $connection) {
echo "> [{$connection->getRemoteName()}] Client disconnected\n";
})->onText(function ($server, $connection, $message) {
Expand Down
6 changes: 6 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,12 @@ public function connect(): void
return;
}
$this->logger->info("[client] Client connected to {$this->socketUri}");
$this->dispatch('handshake', [
$this,
$this->connection,
$this->connection->getHandshakeRequest(),
$this->connection->getHandshakeResponse(),
]);
$this->dispatch('connect', [$this, $this->connection, $response]);
}

Expand Down
6 changes: 6 additions & 0 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,12 @@ protected function acceptSocket(SocketServer $socket): void
$request = $this->performHandshake($connection);
$this->connections[$name] = $connection;
$this->logger->info("[server] Accepted connection from {$name}.");
$this->dispatch('handshake', [
$this,
$connection,
$connection->getHandshakeRequest(),
$connection->getHandshakeResponse(),
]);
$this->dispatch('connect', [$this, $connection, $request]);
} catch (Exception $e) {
if ($connection) {
Expand Down
7 changes: 7 additions & 0 deletions src/Trait/ListenerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ trait ListenerTrait
{
private array $listeners = [];

/* @todo: Deprecate and remove in v4 */
public function onConnect(Closure $closure): self
{
$this->listeners['connect'] = $closure;
Expand All @@ -29,6 +30,12 @@ public function onDisconnect(Closure $closure): self
return $this;
}

public function onHandshake(Closure $closure): self
{
$this->listeners['handshake'] = $closure;
return $this;
}

public function onText(Closure $closure): self
{
$this->listeners['text'] = $closure;
Expand Down
10 changes: 9 additions & 1 deletion tests/suites/client/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
ClientException
};
use WebSocket\Http\{
Request,
Response
};
use WebSocket\Test\MockStreamTrait;
Expand Down Expand Up @@ -821,6 +822,13 @@ public function testListeners(): void
$client = new Client('ws://localhost:8000/my/mock/path');
$client->setStreamFactory(new StreamFactory());

$client->onHandshake(function ($client, $connection, $request, $response) {
$this->assertInstanceOf(Client::class, $client);
$this->assertInstanceOf(Connection::class, $connection);
$this->assertInstanceOf(Request::class, $request);
$this->assertInstanceOf(Response::class, $response);
$this->assertTrue($client->isRunning());
});
$client->onConnect(function ($client, $connection, $response) {
$this->assertInstanceOf(Client::class, $client);
$this->assertInstanceOf(Connection::class, $connection);
Expand Down Expand Up @@ -959,7 +967,7 @@ public function testAlreadyStarted(): void
$client = new Client('ws://localhost:8000/my/mock/path');
$client->setStreamFactory(new StreamFactory());

$client->onConnect(function ($client, $connection, $request) {
$client->onHandshake(function ($client, $connection, $request, $response) {
$client->start();
$client->stop();
});
Expand Down
11 changes: 9 additions & 2 deletions tests/suites/server/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
ServerException
};
use WebSocket\Http\{
Response,
ServerRequest
};
use WebSocket\Message\{
Expand Down Expand Up @@ -77,6 +78,12 @@ public function testListeners(): void
$this->assertInstanceOf(Stringable::class, $server);
$this->assertEquals('WebSocket\Server(closed)', "{$server}");

$server->onHandshake(function ($server, $connection, $request, $response) {
$this->assertInstanceOf(Server::class, $server);
$this->assertInstanceOf(Connection::class, $connection);
$this->assertInstanceOf(ServerRequest::class, $request);
$this->assertInstanceOf(Response::class, $response);
});
$server->onConnect(function ($server, $connection, $request) {
$this->assertInstanceOf(Server::class, $server);
$this->assertInstanceOf(Connection::class, $connection);
Expand Down Expand Up @@ -335,7 +342,7 @@ public function testDetachConnection(): void
$server = new Server(8000);
$server->setStreamFactory(new StreamFactory());

$server->onConnect(function ($server, $connection, $request) {
$server->onHandshake(function ($server, $connection, $request, $response) {
$connection->disconnect();
$server->stop();
});
Expand Down Expand Up @@ -388,7 +395,7 @@ public function testAlreadyStarted(): void
$server = new Server(8000);
$server->setStreamFactory(new StreamFactory());

$server->onConnect(function ($server, $connection, $request) {
$server->onHandshake(function ($server, $connection, $request, $response) {
$connection->disconnect();
$server->start();
$server->stop();
Expand Down