You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+70-17Lines changed: 70 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,11 @@
1
1
# amphp/websocket-server
2
2
3
3
AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind.
4
-
This library provides a [`RequestHandler`](https://amphp.org/http-server/classes/request-handler) to easily handle Websocket connections using [`amphp/http-server`](https://github.com/amphp/http-server).
4
+
This library provides a [`RequestHandler`](https://amphp.org/http-server/classes/request-handler) to easily handle WebSocket connections using [`amphp/http-server`](https://github.com/amphp/http-server).
The documentation for this library is currently a work in progress. Pull Requests to improve the documentation are
17
-
always welcome!
20
+
The primary component of this library is the `Websocket` class, an implementation of the `RequestHandler` interface from [`amphp/http-server`](https://github.com/amphp/http-server). Endpoints using the `Websocket` request handler will upgrade incoming requests to a WebSocket connection.
18
21
19
-
## Requirements
22
+
Creating a `Websocket` endpoint requires the user to specify a number of parameters:
23
+
- The `Amp\Http\Server\HttpServer` instance which will be used
24
+
- A [PSR-3](https://www.php-fig.org/psr/psr-3/) logger instance
25
+
- A `WebsocketAcceptor` to accept client connections
26
+
- A `WebsocketClientHandler` to handle client connections once accepted
27
+
- An optional `WebsocketCompressionContextFactory` if compression should be enabled on the server
28
+
- An optional `WebsocketClientFactory` if custom logic is needed when creating `WebsocketClient` instances
20
29
21
-
- PHP 8.1+
30
+
### Accepting Client Connections
31
+
32
+
Accepting client connections is performed by an instance of `WebsocketAcceptor`. This library provides two implementations: and `AllowOriginAcceptor`
33
+
-`Rfc6455Acceptor`: Accepts client connections based on [RFC6455](https://datatracker.ietf.org/doc/html/rfc6455) with no further restrictions.
34
+
-`AllowOriginAcceptor`: Requires the `"Origin"` header of the HTTP request to match one of the allowed origins provided to the constructor. Accepting the connection is then delegated to another `WebsocketAcceptor` implementation (`Rfc6455Acceptor` by default).
35
+
36
+
### Handling Client Connections
37
+
38
+
Once established, a WebSocket connection is handled by an implementation of `WebsocketClientHandler`. This is where your WebSocket application logic will go.
39
+
40
+
`WebsocketClientHanler` has a single method which must be implemented, `handleClient()`.
41
+
42
+
```php
43
+
public function handleClient(
44
+
WebsocketClient $client,
45
+
Request $request,
46
+
Response $response,
47
+
): void;
48
+
```
22
49
23
-
## Example
50
+
After accepting a client connection, `WebsocketClientHandler::handleClient()` is invoked with the `WebsocketClient` instance, as well as the `Request` and `Response` instances which were used to establish the connection.
51
+
52
+
This method should not return until the client connection should be closed. This method generally should not throw an exception. Any exception thrown will close the connection with an `UNEXPECTED_SERVER_ERROR` error code (1011) and forward the exception to the HTTP server logger. There is one exception to this: `WebsocketClosedException`, which is thrown when receiving or sending a message to a connection fails due to the connection being closed. If `WebsocketClosedException` is thrown from `handleClient()`, the exception is ignored.
53
+
54
+
### Gateways
55
+
56
+
A `WebsocketGateway` provides a means of collecting WebSocket clients into related groups to allow broadcasting a single message efficiently (and asynchronously) to multiple clients. `WebsocketClientGateway` provided by this library may be used by one or more client handlers to group clients from one or more endpoints (or multiple may be used on a single endpoint if desired). See the [example server](#example-server) below for basic usage of a gateway in a client handler. Clients added to the gateway are automatically removed when the client connection is closed.
57
+
58
+
### Compression
59
+
60
+
Message compression may optionally be enabled on individual WebSocket endpoints by passing an instance of `WebsocketCompressionContextFactory` to the `Websocket` constructor. Currently, the only implementation available is `Rfc7692CompressionFactory` which implements compression based on [RFC-7692](https://datatracker.ietf.org/doc/html/rfc7692).
61
+
62
+
### Example Server
63
+
64
+
The server below creates a simple WebSocket endpoint which broadcasts all received messages to all other connected clients. [`amphp/http-server-router`](https://github.com/amphp/http-server-router) and [`amphp/http-server-static-content`](https://github.com/amphp/http-server-static-content) are used to attach the `Websocket` handler to a specific route and to serve static files from the `/public` directory if the route is not defined in the router.
24
65
25
66
```php
26
67
<?php
@@ -44,13 +85,13 @@ use Amp\Websocket\Server\WebsocketClientHandler;
0 commit comments