Skip to content

Commit

Permalink
Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
sirn-se committed Jan 20, 2024
1 parent 6d21dfb commit c184192
Show file tree
Hide file tree
Showing 15 changed files with 207 additions and 169 deletions.
File renamed without changes.
10 changes: 9 additions & 1 deletion docs/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
[Documentation](Index.md) > Changelog
[Documentation](Index.md) / Changelog

# Websocket: Changelog

## `v2.2`

> PHP version `^8.0`
### `2.2.0`

* Documentation overview (@sirn-se)

## `v2.1`

> PHP version `^8.0`
Expand Down
2 changes: 1 addition & 1 deletion docs/Client.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Documentation](Index.md) > Client
[Documentation](Index.md) / Client

# Websocket: Client

Expand Down
3 changes: 2 additions & 1 deletion docs/Contributing.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Documentation](Index.md) > Contributing
[Documentation](Index.md) / Contributing

# Websocket: Contributing

Expand All @@ -16,6 +16,7 @@ Base your patch on corresponding version branch, and target that version branch

| Version | Branch | PHP | Status |
| --- | --- | --- | --- |
| [`2.2`](https://github.com/sirn-se/websocket-php/tree/2.2.0) | `v2.2-main` | `^8.0` | Future version |
| [`2.1`](https://github.com/sirn-se/websocket-php/tree/2.1.0) | `v2.1-main` | `^8.0` | Current version |
| [`2.0`](https://github.com/sirn-se/websocket-php/tree/2.0.0) | `v2.0-main` | `^8.0` | Bug fixes only |
| [`1.7`](https://github.com/sirn-se/websocket-php/tree/1.7.0) | `v1.7-master` | `^7.4\|^8.0` | Bug fixes only |
Expand Down
2 changes: 1 addition & 1 deletion docs/Examples.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Documentation](Index.md) > Examples
[Documentation](Index.md) / Examples

# Websocket: Examples

Expand Down
1 change: 1 addition & 0 deletions docs/Index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
* [Changelog](Changelog.md) - The changelog of this repo
* [Contributing](Contributing.md) - Contributors and requirements
* [Examples](Examples.md) - Examples
* [License](../LICENCE.md) - License
2 changes: 1 addition & 1 deletion docs/Listener.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Documentation](Index.md) > Listener
[Documentation](Index.md) / Listener

# Websocket: Listener

Expand Down
2 changes: 1 addition & 1 deletion docs/Message.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Documentation](Index.md) > Message
[Documentation](Index.md) / Message

# Websocket: Message

Expand Down
171 changes: 9 additions & 162 deletions docs/Middleware.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Documentation](Index.md) > Middleware
[Documentation](Index.md) / Middleware

# Websocket: Middleware

Expand All @@ -24,172 +24,19 @@ $server
These two middlewares provide standard operability according to WebSocket protocol,
and should be added unless you write your own implementation of close and ping/pong handling.

* `CloseHandler` - Automatically acts on incoming and outgoing Close requests, as specified in WebSocket protocol
* `PingResponder` - Responds with Pong message when receiving a Ping message, as specified in WebSocket protocol
* [CloseHandler](Middleware/CloseHandler.md) - Automatically acts on incoming and outgoing Close requests, as specified in WebSocket protocol
* [PingResponder](Middleware/PingResponder.md) - Responds with Pong message when receiving a Ping message, as specified in WebSocket protocol

## Optional middlewares

These middlewares are included in library and can be added to provide additional functionality.

* `PingInterval` - Used to automatically send Ping messages at specified interval
* `Callback` - Apply provided callback function on specified actions
* [PingInterval](Middleware/PingInterval.md) - Used to automatically send Ping messages at specified interval
* [Callback](Middleware/Callback.md) - Apply provided callback function on specified actions

## Middleware descriptions
## Creating your own middleware

Description of included middlewares.
You can create your own middleware by implementing relevant interfaces.
A middleware may handle WebSocket message transfers, HTTP handshake operations, and Tick operability.

### The CloseHandler middleware

* When a Close message is received, CloseHandler will respond with a Close confirmation message
* When a Close confirmation message is received, CloseHandler will close the connection
* When a Close message is sent, CloseHandler will block further messages from being sent

```php
$client_or_server->addMiddleware(new WebSocket\Middleware\CloseHandler());
```

### The PingResponder middleware

* When a Ping message is received, PingResponder will respond with a Pong message

```php
$client_or_server->addMiddleware(new WebSocket\Middleware\PingResponder());
```

### The PingInterval middleware

* Sends Ping messages on Connection at interval, typically used as "heartbeat" to keep connection open
* If `interval` is not specified, it will use connection timeout configuration as interval

```php
$client_or_server->addMiddleware(new WebSocket\Middleware\PingInterval(interval: 10));
```

### The Callback middleware

This middleware will apply callback functions on certain actions.

```php
$client_or_server
->addMiddleware(new WebSocket\Middleware\Callback(
incoming: function (WebSocket\Middleware\ProcessStack $stack, WebSocket\Connection $connection): WebSocket\Message\Message {
$message = $stack->handleIncoming(); // Get incoming message from next middleware
$message->setContent("Changed message");
return $message;
},
outgoing: function (WebSocket\Middleware\ProcessStack $stack, WebSocket\Connection $connection, WebSocket\Message\Message $message): WebSocket\Message\Message {
$message->setContent('Changed message');
$message = $stack->handleOutgoing($message); // Forward outgoing message to next middleware
return $message;
},
httpIncoming: function (WebSocket\Middleware\ProcessHttpStack $stack, WebSocket\Connection $connection): WebSocket\Http\Message {
$message = $stack->handleHttpIncoming(); // Get incoming message from next middleware
return $message;
},
httpOutgoing: function (WebSocket\Middleware\ProcessHttpStack $stack, WebSocket\Connection $connection, WebSocket\Http\Message $message): WebSocket\Http\Message {
$message = $stack->handleHttpOutgoing($message); // Forward outgoing message to next middleware
return $message;
},
tick: function (WebSocket\Middleware\ProcessTickStack $stack, WebSocket\Connection $connection): void {
$stack->handleTick(); // Forward tick to next middleware
}
));
```

* The `incoming` and `outgoing` callbacks **MUST** return a [Message](Message.md) instance
* The `httpIncoming` and `httpOutgoing` callbacks **MUST** return a HTTP request/response instance respectively
* The `tick` callback returns nothing

The `handleIncoming`, `handleOutgoing`, `handleHttpIncoming`, `handleHttpOutgoing` and `handleTick` methods will pass initiative further down the middleware stack.

## Writing your own middleware

A middleware **MUST** implement the `MiddlewareInterface`.

```php
interface WebSocket\Middleware\MiddlewareInterface
{
public function __toString(): string;
}
```

A middleware that wants to handle incoming messages **MUST** implement the `ProcessIncomingInterface`.

```php
interface WebSocket\Middleware\ProcessIncomingInterface extends WebSocket\Middleware\MiddlewareInterface
{
public function processIncoming(
WebSocket\Middleware\ProcessStack $stack,
WebSocket\Connection $connection
): WebSocket\Message\Message;
}
```

A middleware that wants to handle outgoing messages **MUST** implement the `ProcessOutgoingInterface`.

```php
interface WebSocket\Middleware\ProcessOutgoingInterface extends WebSocket\Middleware\MiddlewareInterface
{
public function processOutgoing(
WebSocket\Middleware\ProcessStack $stack,
WebSocket\Connection $connection,
WebSocket\Message\Message $message
): WebSocket\Message\Message;
}
```

A middleware that wants to handle incoming HTTP messages **MUST** implement the `ProcessHttpIncomingInterface`.

```php
interface WebSocket\Middleware\ProcessHttpIncomingInterface extends WebSocket\Middleware\MiddlewareInterface
{
public function processHttpIncoming(
WebSocket\Middleware\ProcessHttpStack $stack,
WebSocket\Connection $connection
): WebSocket\Http\Message;
}
```

A middleware that wants to handle outgoing HTTP messages **MUST** implement the `ProcessHttpOutgoingInterface`.

```php
interface WebSocket\Middleware\ProcessHttpOutgoingInterface extends WebSocket\Middleware\MiddlewareInterface
{
public function processHttpOutgoing(
WebSocket\Middleware\ProcessHttpStack $stack,
WebSocket\Connection $connection,
WebSocket\Http\Message $message
): WebSocket\Http\Message;
}
```

A middleware that wants to handle Tick operation **MUST** implement the `ProcessTickInterface`.

```php
interface WebSocket\Middleware\ProcessTickInterface extends WebSocket\Middleware\MiddlewareInterface
{
public function processTick(
WebSocket\Middleware\ProcessTickStack $stack,
WebSocket\Connection $connection
): void;
}
```

The `ProcessStack`, `ProcessHttpStack` and `ProcessTickStack` classes are used to hand over initiative to the next middleware in middleware stack.

```php
// Get the received Message, possibly handled by other middlewares
$message = $stack->handleIncoming();

// Forward the Message to be sent, possibly handled by other middlewares
$message = $stack->handleOutgoing($message);

// Get the received HTTP request/response message, possibly handled by other middlewares
$message = $stack->handleHttpIncoming();

// Forward the HTTP request/response message to be sent, possibly handled by other middlewares
$message = $stack->handleHttpOutgoing($message);

// Forward the Tick operation, possibly handled by other middlewares
$stack->handleTick();
```
* [Creating](Middleware/Creating.md) - How to create a Middleware
39 changes: 39 additions & 0 deletions docs/Middleware/Callback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[Documentation](Index.md) / [Middleware](Middleware.md) / Callback

# Websocket: Callback middleware

This middleware will apply callback functions on certain actions.
This middlewares is included in library and can be added to provide additional functionality.

```php
$client_or_server
->addMiddleware(new WebSocket\Middleware\Callback(
incoming: function (WebSocket\Middleware\ProcessStack $stack, WebSocket\Connection $connection): WebSocket\Message\Message {
$message = $stack->handleIncoming(); // Get incoming message from next middleware
$message->setContent("Changed message");
return $message;
},
outgoing: function (WebSocket\Middleware\ProcessStack $stack, WebSocket\Connection $connection, WebSocket\Message\Message $message): WebSocket\Message\Message {
$message->setContent('Changed message');
$message = $stack->handleOutgoing($message); // Forward outgoing message to next middleware
return $message;
},
httpIncoming: function (WebSocket\Middleware\ProcessHttpStack $stack, WebSocket\Connection $connection): WebSocket\Http\Message {
$message = $stack->handleHttpIncoming(); // Get incoming message from next middleware
return $message;
},
httpOutgoing: function (WebSocket\Middleware\ProcessHttpStack $stack, WebSocket\Connection $connection, WebSocket\Http\Message $message): WebSocket\Http\Message {
$message = $stack->handleHttpOutgoing($message); // Forward outgoing message to next middleware
return $message;
},
tick: function (WebSocket\Middleware\ProcessTickStack $stack, WebSocket\Connection $connection): void {
$stack->handleTick(); // Forward tick to next middleware
}
));
```

* The `incoming` and `outgoing` callbacks **MUST** return a [Message](Message.md) instance
* The `httpIncoming` and `httpOutgoing` callbacks **MUST** return a HTTP request/response instance respectively
* The `tick` callback returns nothing

The `handleIncoming`, `handleOutgoing`, `handleHttpIncoming`, `handleHttpOutgoing` and `handleTick` methods will pass initiative further down the middleware stack.
14 changes: 14 additions & 0 deletions docs/Middleware/CloseHandler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[Documentation](Index.md) / [Middleware](Middleware.md) / CloseHandler

# Websocket: CloseHandler middleware

Thid middleware provide standard operability according to WebSocket protocol,
and should be added unless you write your own implementation of close handling.

* When a Close message is received, CloseHandler will respond with a Close confirmation message
* When a Close confirmation message is received, CloseHandler will close the connection
* When a Close message is sent, CloseHandler will block further messages from being sent

```php
$client_or_server->addMiddleware(new WebSocket\Middleware\CloseHandler());
```
Loading

0 comments on commit c184192

Please sign in to comment.